Search code examples
logginglog4jrolloverbridge

Log4j not rolling over with latest version log4j-2.17.2


I have a log4j configuration that's intended to roll files once daily. It is working fine with the log4j-2.17.0 and 2.17.1. When I update to the latest version 2.17.2, the daily logs stop rolling over.

Here is my log4j.properties:

log4j.rootLogger=INFO, A2

log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.threshold=debug
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

log4j.appender.A2=org.apache.log4j.RollingFileAppender
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%d [%t] %-5p - %m%n
log4j.appender.A2.DatePattern='.'MMdd
log4j.appender.A2.File=c:/test/log/test.log
log4j.appender.A2.filePattern=c:/test/log/test.log.%d{MMdd}

The 2 switches I added are -Dlog4j.configuration=C:/test/log4j.properties and -Dlog4j1.compatibility=true

Does anyone know why this would stop rolling over daily when upgrading from log4j-2.17.1 to log4j-2.17.2 and what changes I need to make to get it to work with 2.17.2?


Solution

  • The RollingFileAppender from Log4j 1.x (cf. javadoc) never supported time based rotations nor did it support the datePattern and filePattern properties.

    The Log4j 1.x bridge 2.17.1 had a bug that caused time based and size based rotations to occur (cf. this question). This was fixed in version 2.17.2.

    If you want time based rotations you need:

    • either configure a DailyRollingFileAppender:
      log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender
      log4j.appender.A2.layout=org.apache.log4j.PatternLayout
      log4j.appender.A2.layout.ConversionPattern=%d [%t] %-5p - %m%n
      log4j.appender.A2.DatePattern=.MMdd
      log4j.appender.A2.File=c:/test/log/test.log
      
    • or configure directly a Log4j2 RollingFileAppender using a Log4j2 configuration file:
      <RollingFileAppender name="A2"
                           fileName="C:\test\log\test.log"
                           filePattern="C:\test\log\test.log.%d{MMdd}">
          <PatternLayout pattern="%d [%t] %-5p %c - %m%n" />
          <TimeBasedTriggeringPolicy />
      </RollingFileAppender>
      

    Under the hood a Log4j2 RollingFileAppender will be used in both cases, but if you use a Log4j2 configuration file, you will have access to all the configuration options of the appender.