Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In log4j 1.2 to log4j 2 migration, what to do with the DailyRollingFileAppender class?

I'm working on migrating a Java project from using log4j 1.2 for logging to using log4j 2.

log4j 1.x has a class org.apache.log4j.DailyRollingFileAppender which is mentioned in my project's log4j.properties configuration file. A class with the same name DailyRollingFileAppender no longer exist in log4j 2. I wonder if DailyRollingFileAppender's role can be replaced by some other class(es) or some replacement configuration or implementation in log4j 2.

The migration guide emphasizes that the application must not access methods and classes internal to log4j 1.x such as Appenders for it to be migratible to log4j 2. But in order to do the migration, what can be done to DailyRollingFileAppender? Is it possible to replace it by some custom configuration, or combination of classes in log4j 2? There are some configuration examples in the migration guide, in particular, one with FileAppender, but what about DailyRollingFileAppender? It's something fairly close to the basic FileAppender:

From log4j 1.2 API Javadoc, class org.apache.log4j.DailyRollingFileAppender

DailyRollingFileAppender extends FileAppender so that the underlying file is rolled over at a user chosen frequency. DailyRollingFileAppender has been observed to exhibit synchronization issues and data loss. The log4j extras companion includes alternatives which should be considered for new deployments and which are discussed in the documentation for org.apache.log4j.rolling.RollingFileAppender.

like image 265
Meng Lu Avatar asked Jun 17 '15 05:06

Meng Lu


1 Answers

You are looking for the RollingFile appender

<RollingFile name="DAILY_LOG" fileName="log/daily.log"
                 filePattern="log/%d{ddMMyyyy}_daily.log"
                 >
      <PatternLayout pattern="%d [%7r] %5p - %c - %m%n"/>
      <Policies>
        <TimeBasedTriggeringPolicy interval="1"/>
      </Policies>                               
</RollingFile>

The previous sample rolls over by day, the interval being 1 unit which is determined by the smallest unit of the date lookup in the file pattern. In other words if the date pattern was d{MMyyyy},then the interval=1 corresponds to a rollover period of 1 month.

Please note there currently is no support for limiting the number of log files kept past a certain date or age etc. You can only limit the number of log files per time period.

like image 178
alan7678 Avatar answered Sep 29 '22 09:09

alan7678