'Log4j RollingFileAppender writing to previously rolled files

I'm using log4j 2.17.1.

Log4j is rolling files daily but will sometimes write to files it has already rolled. In some cases it is going back several days.

Example:

  • app.log.2022-01-03 has been overwritten with data from 2022-01-04.
  • app.log.2022-01-04 has been overwritten with data from 2022-01-10.
  • app.log.2022-01-11 has been overwritten with data from 2022-01-17.

Is there anything wrong with my configuration here? I just want it to roll everyday.

<Configuration>

  <Appenders>
    <RollingFile name="A1" append="true" fileName="/var/log/app/app.log">
      <PatternLayout
        pattern="%d{yyyy-MM-dd hh:mm:ss} [%t] %-5p %c %x %m%n" />
      <FilePattern>/var/log/app/app.log.%d{yyyy-MM-dd}</FilePattern>
      <Policies>
        <TimeBasedTriggeringPolicy />
      </Policies>
    </RollingFile>
  </Appenders>

  <Loggers>

    <Root level="info">
      <AppenderRef ref="A1" />
    </Root>

  </Loggers>

</Configuration>


Solution 1:[1]

I believe my issue was caused by multiple LoggerContexts attempting to write to the same RollingFileAppender.

I resolved this problem by following the steps in the Log4j Web Application usage:

https://logging.apache.org/log4j/2.x/manual/webapp.html

I had several web applications deployed to a single JBoss instance. Each app had their own copy of the log4j jars. I moved the log4j jars out of the wars and into a JBoss module to get them into the server classloader.

Then I followed the directions on the Logging Separation page:

https://logging.apache.org/log4j/2.x/manual/logsep.html

and I configured a single LoggerContext for JBoss:

Place the logging jars in the container's classpath and set the system property log4j2.contextSelector to org.apache.logging.log4j.core.selector.BasicContextSelector. This will create a single LoggerContext using a single configuration that will be shared across all applications.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rob Benton