Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j: How to configure simplest possible file logging?

My story:

I want to make a thing which is as simple as a simplest possible log4j logger that logs rows to a file. I have found several examples with some functionality, but not a basic, general one that really works, and not one with an explanation how the each row work.

Question:

Could anybody provide one?

Prerequisites:

  • I already know where to put the file and I have the log4j configured and working for console logging.
  • Now I want to log to a file and also find the file from file system once the program has run.
  • Rows needed to be added to the existing log4j.properties file are the desired output.
like image 567
mico Avatar asked Jun 15 '11 13:06

mico


People also ask

How do you set a log4j properties file location?

The file is named log4j. properties and is located in the $DGRAPH_HOME/dgraph-hdfs-agent/lib directory. The file defines the ROLLINGFILE appenders for the root logger and also sets the log level for the file. The level of the root logger is defined as INFO and attaches the ROLLINGFILE appender to it.

How do you set log file path in log4j properties dynamically?

System. setProperty("{my. log", "C:/logfile. log");


1 Answers

I have one generic log4j.xml file for you:

<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" > <log4j:configuration debug="false">      <appender name="default.console" class="org.apache.log4j.ConsoleAppender">         <param name="target" value="System.out" />         <param name="threshold" value="debug" />         <layout class="org.apache.log4j.PatternLayout">             <param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] - %m%n" />         </layout>     </appender>      <appender name="default.file" class="org.apache.log4j.FileAppender">         <param name="file" value="/log/mylogfile.log" />         <param name="append" value="false" />         <param name="threshold" value="debug" />         <layout class="org.apache.log4j.PatternLayout">             <param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] - %m%n" />         </layout>     </appender>      <appender name="another.file" class="org.apache.log4j.FileAppender">         <param name="file" value="/log/anotherlogfile.log" />         <param name="append" value="false" />         <param name="threshold" value="debug" />         <layout class="org.apache.log4j.PatternLayout">             <param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] - %m%n" />         </layout>     </appender>      <logger name="com.yourcompany.SomeClass" additivity="false">         <level value="debug" />         <appender-ref ref="another.file" />     </logger>      <root>         <priority value="info" />         <appender-ref ref="default.console" />         <appender-ref ref="default.file" />     </root> </log4j:configuration> 

with one console, two file appender and one logger poiting to the second file appender instead of the first.

EDIT

In one of the older projects I have found a simple log4j.properties file:

# For the general syntax of property based configuration files see # the documentation of org.apache.log4j.PropertyConfigurator.  # The root category uses two appenders: default.out and default.file. # The first one gathers all log output, the latter only starting with  # the priority INFO. # The root priority is DEBUG, so that all classes can be logged unless  # defined otherwise in more specific properties. log4j.rootLogger=DEBUG, default.out, default.file  # System.out.println appender for all classes log4j.appender.default.out=org.apache.log4j.ConsoleAppender log4j.appender.default.out.threshold=DEBUG log4j.appender.default.out.layout=org.apache.log4j.PatternLayout log4j.appender.default.out.layout.ConversionPattern=%-5p %c: %m%n  log4j.appender.default.file=org.apache.log4j.FileAppender log4j.appender.default.file.append=true log4j.appender.default.file.file=/log/mylogfile.log log4j.appender.default.file.threshold=INFO log4j.appender.default.file.layout=org.apache.log4j.PatternLayout log4j.appender.default.file.layout.ConversionPattern=%-5p %c: %m%n 

For the description of all the layout arguments look here: log4j PatternLayout arguments

like image 50
Tomasz Stanczak Avatar answered Sep 28 '22 03:09

Tomasz Stanczak