Wednesday, April 13, 2016

The log4j2 configuration naming and other tips

Customizing of log4j2 configuration is a big issue. This post gives just small tips for:
  • log4j2 configuration file naming
  • PatternLayout
  • File rolling

The log4j2 configuration file naming

The log4j2 executes automatic detection of logging configuration. The manual explains in which order this is done.

The best practice is to use the default configuration file log4j2.xml and for JUnits log4j2-test.xml.

Change of the configuration file to the different name should be done with the system property log4j.configurationFile:

Example 1. Setting of log configuration file for specific JUnit:
public static void initLog4j() {
     System.setProperty("log4j.configurationFile", "app-log4j2.xml");

Example 2. Setting of log configuration file via JVM properties:

PatternLayout tips

1. The whole pattern should be written in a single line; each line of the pattern is interpolated into the line of a log message.
2. To print date in GMT format use pattern:
%d{EEE, dd MMM yyyy HH':'mm':'ss }{GMT + 0} GMT %m%n
3.The double sign $$ before a property or lookup in the format pattern causes value interpolation each time the pattern is used. If a single $ is mentioned in the format pattern, the value is interpolated only once.
4.Multi-line formatting should be done with the lookup ${sys:line.separator}. This for example is useful for formatting of the header or footer.

File rolling example

The log4j2 provides several mechanisms of file rolling.
File rolling is reached with the RollingFileAppender, which uses one or more triggering policy. The manual gives several good examples.

Below is shown one more example. It demonstrates RollingFileAppender with two triggering polices: time and size-based.
The appender will create up to 21 archives on the same day (1-21); the oldest file with the highest index.
Since the max size of file is small (1 KB), several files may be created for rather small application. This may be used for testing of the rolling.
Note: If uncomment the OnStartupTriggeringPolicy, a new log will be created upon each restart.

The log4j2.xml:
<configuration monitorinterval="300">  
           <property name="logFile">app-demo</property>  
           <property name="logDir">logs</property>  
           <rollingfile filename="${logDir}/${logFile}.log" filepattern="${logDir}/${logFile}-%d{yyyy-MM-dd}-%i.log" name="RollingFile">  
                          %d %p (%c{1.}.%M:%L) - %m%n  
                     <!-- <OnStartupTriggeringPolicy/> -->  
                     <timebasedtriggeringpolicy interval="10">  
                     <sizebasedtriggeringpolicy size="1 KB">                      
                <defaultrolloverstrategy fileindex="min" max="21">  
           <root level="info">  
                <appenderref ref="RollingFile">  

No comments :

About the author

My Photo
I trust only simple code and believe that code should be handsome. This is not a matter of technology, but professional approach, consolidated after years of software development. I enjoy to cause things working and feel very happy, when I manage to solve a problem.
Back to Top