I am using RollingFile appender. I want the log file to be rolled after every 20 minutes irrespective of the logging event. For instance in an hour I should have 3 log files even though there might have not been any logging in that hour. Is this possible using Log4j2? If yes please provide the configuration ( in log4j2.xml) that are required.
The below config does not seem to work :
<RollingFile name="RECHARGE_NMCD" fileName="D:/rc_nmcd/rc_nmcd.log" append="true" bufferedIO="false" filePattern="D:/rc_nmcd/rc_nmcd_%d{yyyy-MM-dd-HH-mm}.process">
<PatternLayout>
<Pattern>%m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="20"/>
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
I don't think you can make Log4J2 roll every N minutes out of the box, it looks like you can get it to do this every minute, hour, day but not 20 minutes. (See https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/DailyRollingFileAppender.html - you can change it to "every minute" using a different date pattern)
I've not tried this, but there might be a way of customising this by providing a custom Rollover Strategy...
https://logging.apache.org/log4j/2.x/log4j-core/apidocs/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.html
If this works, please post your answer for other people to learn from!
We may can do this using Corn Expression policy
CronTriggeringPolicy schedule="0 0/20 * 1/1 * ? *"/>.
This will roll your file automatically every after 20 minutes, irrespective of logging event.
try this :
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<fileNamePattern>foo.%d{yyyyMMdd-HHmm}.gz</fileNamePattern>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
<maxHistory>20</maxHistory>
</rollingPolicy>
instead of
<Policies>
<TimeBasedTriggeringPolicy interval="20"/>
</Policies>
I referred this plugin https://github.com/mushkevych/log4j2plugin
I had a Runnable thread per FTimeBasedTriggeringPolicy which would actually sleep upto next rollover instead of the LogRotateThread which sleeps for some indefinite specified time.
Thread rotateThread = new Thread(new LogRotateRunnable(this));
rotateThread.start();
Added the above after initialize(RollingFileManager)
LogRotateRunnable :
while (true) {
long sleepTime = fTimeBasedTriggeringPolicy.getNextRollover()
- System.currentTimeMillis();
if (sleepTime > 0) {
try {
Thread.sleep(sleepTime + EMPTY_LOG_EVENT_DELAY);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
fTimeBasedTriggeringPolicy.checkRollover(new EmptyLogEvent());
}
Also it won't roll empty files, but the good part is of course if atleast one valid log entry within next rollover time, it will.