Reputation: 63
I am using log4j2 Rolling File appender for logging. The traffic for the logger is quite sparse. I want the logger to rollover the file at the appropriate time. How can i do that ?
This is the output of ls -lrth command on the log files.
-rw-r--r-- 1 root root 136 Apr 27 15:51 logfile-04-20150427-07-00.log.gz
-rw-r--r-- 1 root root 133 Apr 27 23:18 logfile-04-20150427-15-00.log.gz
-rw-r--r-- 1 root root 151 Apr 28 04:40 logfile-04-20150427-23-00.log.gz
-rw-r--r-- 1 root root 161 Apr 28 05:14 logfile-04-20150428-04-00.log.gz
-rw-r--r-- 1 root root 134 Apr 28 06:45 logfile-04-20150428-05-00.log.gz
-rw-r--r-- 1 root root 125 Apr 28 08:46 logfile-04-20150428-06-00.log.gz
-rw-r--r-- 1 root root 191 Apr 28 09:27 logfile-04-20150428-08-00.log.gz
-rw-r--r-- 1 root root 281 Apr 28 10:43 logfile-04-20150428-09-00.log.gz
Clearly the logger is not rotating the logfile at the apt time. (see the modification time of the file and the logfile timestamp). The following is my log4j2.xml configuration for the logger.
<RollingFile name="userlogfileAppender"
fileName="${sys:catalina.home}/webapps/miscLogs/uData/logfile/logfile.log"
filePattern="${sys:catalina.home}/webapps/miscLogs/uData/logfile/logfile-${logfileId}-%d{yyyyMMdd-HH}-00.log.gz"
immediateFlush="true"
bufferedIO="false">
<PatternLayout>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
<logger additivity="false" name="userlogfileLogger" level="debug">
<AppenderRef ref="userlogfileAppender"/>
Upvotes: 1
Views: 690
Reputation: 36754
Rollover is not driven by a timer in log4j2, but by log events. The rollover appender will compare the timestamp of the log event with the scheduled rollover time, and if the rollover time has been exceeded, then the file is rolled over.
This means that if traffic is sparse, there will often not be a log event that triggers a rollover at the scheduled rollover time. Rollover will occur at the next event after the scheduled rollover time, which may be hours later.
Log4j2 currently does not have a timer mechanism to force rollover. Depending on how mission-critical this is, you can create a timer in your application that logs an event once an hour to force a rollover.
Upvotes: 1