Skip to content
Advertisement

Log4j RollingFileAppender has odd behavior when using the Log4j 1.x bridge

While I migrate to log4j2, I have configured my Tomcat web application to use the Log4j 1.x bridge. I followed the Migration guide here: https://logging.apache.org/log4j/2.x/manual/migration.html

I continue to use my existing log4j.properties file which look like this:

log4j.rootLogger=INFO, file

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/var/log/mylog.out
log4j.appender.file.MaxFileSize=10KB
log4j.appender.file.MaxBackupIndex=2
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%%d{ISO8601} %%-5p %%t %%c - %%m%%n

This successfully creates my log file and rolls over at 10KB.

I am having an issues that I can’t explain. Here are the files in my log directory:

-rw-r----- 1 root root 7.6K Jan 10 12:27 test.log
-rw-r----- 1 root root 11K Jan 10 12:27 test.log.1
-rw-r----- 1 root root 36K Jan 10 12:27 test.log.2
-rw-r----- 1 root root 11K Jan 10 12:27 test.log2022-01-10

I get a log file named for the current day. I’ve experimented and it looks like the RollingFileAppender is also rolling at the end of each day. It didn’t do this before using the bridge. Any idea why this is happening and how to stop it?

Advertisement

Answer

Edit: This was a bug and was fixed in version 2.17.2.

This is a bug and you should report it. The RollingFileAppenderBuilder (cf. source code) has a filePattern containing a date instead of a number:

        String filePattern = fileName +"%%d{yyy-MM-dd}";
        TriggeringPolicy timePolicy = TimeBasedTriggeringPolicy.newBuilder().setModulate(true).build();
        SizeBasedTriggeringPolicy sizePolicy = SizeBasedTriggeringPolicy.createPolicy(maxSize);
        CompositeTriggeringPolicy policy = CompositeTriggeringPolicy.createPolicy(sizePolicy, timePolicy);
        RolloverStrategy strategy = DefaultRolloverStrategy.newBuilder()
                .setConfig(config)
                .setMax(maxBackups)
                .build();
        return new AppenderWrapper(RollingFileAppender.newBuilder()
                .setName(name)
                .setConfiguration(config)
                .setLayout(fileLayout)
                .setFilter(fileFilter)
                .setBufferedIo(bufferedIo)
                .setImmediateFlush(immediateFlush)
                .setFileName(fileName)
                .setFilePattern(filePattern)
                .setPolicy(policy)
                .setStrategy(strategy)
                .build());

and hence it behaves more like the DailyRollingFileAppender, than a Log4j 1.x RollingFileAppender.

As a workaround you can use the older Log4j1ConfigurationFactory by setting these system properties:

log4j2.configurationFactory=org.apache.log4j.config.Log4j1ConfigurationFactory
log4j2.configurationFile=classpath:log4j.properties

Advertisement