sliks
sliks

Reputation: 111

Log4j2 Always writing to the same file after rollback

I am trying to set up the log so that it will rotate every minute. The date and timestamp works, but once it fires the rollover, the new entry will be written in the previous minute log file. i.e. it did not create a new log file in the next minute.

For example. In the first minute, entries are written to A2018-11-27 11:50.csv On the next minute, it still writes to A2018-11-27 11:50.csv even though it has already created a rollover archive called 2018-11-27 11:50.csv.gz. It should create a new log file A2018-11-27 11:51.csv.

Any suggestion?

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug" monitorInterval="30">
  <Console name="Console" target="SYSTEM_OUT">
    <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
  </Console>
  <Appenders>
    <RollingFile name="HR0" fileName="../logs/m/A${date:yyyy-MM-dd hh:mm}.csv" filePattern="../logs/m/AAA ${date:yyyy-MM-dd hh:mm}.csv">
      <CronTriggeringPolicy schedule="0 * * * * ?" />
    </RollingFile>
  </Appenders>
  <Loggers>
  <Root level="info">
    <AppenderRef ref="Console"/>
  </Root>
  <Logger name="HR0" additivity="false" level="info">
    <AppenderRef ref="HR0" />
  </Logger>
</Configuration>

someJavafile.java

public class someJavafile {
    private final Logger itsLoggerHR0 = LogManager.getLogger("HR0");
    itsLoggerHR0.info("AAA");
}

Upvotes: 0

Views: 809

Answers (1)

sliks
sliks

Reputation: 111

I manage to figure it out based on this jira ticket from log4j2: https://issues.apache.org/jira/browse/LOG4J2-1185

I will post my working solution here. I am using log4j2 2.11.1

The fix is to remove "fileName" and use %d instead of $ in your the filePattern

<RollingFile name="HR0" filePattern="../logs/measure/%d{yyyy-MM-dd hh:mm}.csv">
  <CronTriggeringPolicy schedule="0 * * * * ?" />
</RollingFile>

Upvotes: 1

Related Questions