2

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?

spbarbieri
  • 117
  • 1
  • 8

1 Answers1

3

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
Piotr P. Karwasz
  • 12,857
  • 3
  • 20
  • 43
  • 2
    Piotr created the PR https://github.com/apache/logging-log4j2/pull/710 which has been merged for the next 2.x release. – eso Jan 26 '22 at 09:29
  • Hi, this information is useful. In which version does it have the fix? We are using log4j 2.17.2 and still seeing a remarkably similar issue. – ctangudu May 25 '22 at 04:30
  • It was supposed to be a size based rolling but doesn't seem to be working as expected. May be time based. FYI, I tried adding the 2 system properties as well. – ctangudu May 25 '22 at 05:08
  • 1
    The OP's problem was fixed in 2.17.2. – Piotr P. Karwasz May 25 '22 at 06:59
  • We had similar problem in using the log4j bridge jars sometime back when the log4j vulnerability was reported. We cloned to code, and applied the fix and built in our local, and pushed the changes to our private artifactory and referred it in our project. which fixed the issue. but now it seems to be fixed and available. – Tim Aug 13 '22 at 00:41