1

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?

1 Answers1

0

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.

Piotr P. Karwasz
  • 12,857
  • 3
  • 20
  • 43
  • Thanks, that worked! And, it finally named the rollover log file properly which it wasn't doing in .0 and .1 because it was ignoring the filePattern property in those versions as you stated. I'd upvote your comment but my account is too new apparently. – John Sherlock Apr 29 '22 at 04:19
  • Since the bridge's purpose is to emulate exactly the behavior of Log4j 1.x, the new version does not have a `filePattern` property either. – Piotr P. Karwasz Apr 29 '22 at 06:24
  • But filePattern worked. When it rolled over last night, it named the log file as .log.MMdd which is how I setup filePattern. That was how it was working for us in Log4j 1.x. – John Sherlock Apr 29 '22 at 18:14
  • The file pattern is fixed to ``. If `filePattern` worked in Log4j 1.x, it means that you were using `org.apache.log4j.rolling.RollingFileAppender` (remark the `rolling` package) from Apache Extras. Support for Apache Extras is a work in progress (cf. [LOG4J2-3483](https://issues.apache.org/jira/browse/LOG4J2-3483)). – Piotr P. Karwasz Apr 29 '22 at 19:10