'Force log rollover in tomcat log4j2 even if no traffic

Is there an option in tomcat log4j2 to force the logs to roll over after a defined interval even though there is no traffic at the time of rollover.

Usual behavior of log4j is, it will roll over the logs only if it gets the next subsequent traffic, in case it didn't get any traffic the logs will be stayed in there until new traffic arrives.



Solution 1:[1]

In your log4j2.xml configuration, I assume you have configured a RollingFileAppender. The log4j 2 RollingFileAppender manual page offers more detail, but you can control when the log file rolls over with the "triggering policy". The TimeBasedTriggeringPolicy only fires when a new log event arrives. Based on your description I think this is what you have in your configuration.

What you can use instead is the CronTriggeringPolicy, where you define a schedule at which the log file should be rolled over. The schedule is defined in a cron expression.

One note of caution: I've seen a few bug reports against CronTriggeringPolicy for Log4j 2.6.2. If you experience any issues please use 2.6.1.

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 Remko Popma