Tomcat Log4j Dynamic Logging

Tomcat Log4j Dynamic Logging

apache tomcat

Tomcat Log4j Dynamic Logging

Using log4j with DailyRollingFileAppender in Tomcat? Well, if you run Tomcat as a windows service, you might have noticed that your logs do not rotate daily.  The logs rotate only when the server is restarted. Although it works perfectly for a server on linux machine. But, tomcat when run as a windows service fail to rotate logs. It seems to be a common problem. I googled a lot for a solution, but didn’t find a solution, didn’t even came close to a solution. So, I decided to post this workaround here.

Why does this happen?

Lets assume we have a DailyRollingFileAppender.

log4j.appender.sampleAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.sampleAppender.File=log_file.log
log4j.appender.sampleAppender.Append=true
log4j.appender.sampleAppender.DatePattern=’.’yyyy-MM-dd
log4j.appender.sampleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.sampleAppender.layout.ConversionPattern=%d|%p|%c:%L|[ThreadName:%t]|%m%n

The date pattern used ‘yyyy-MM-dd’ ensures that the logs rotate daily. The log rotation process involves the following steps:
1. The current logs are appended to ‘log_file.log’
2. Once the day is over, log4j tries to rename the file ‘log_file.log’ to ‘log_file.2013-01-14’
3. The new logs then go to ‘log_file.log’

If the ‘log_file.log’ is in use, it will never be renamed and the application will continue to append logs to ‘log_file.log’. This results in a awefully large size of log file.

Workaround! Problem tomcat?

There are several tools which offer to rotate these logs, but most of them failed to do so. There is a work around to this problem though, we can tweak the logging a bit to force the logs to rotate. Since, the cause of the problem is resource not being renamed, .instead of renaming the file just start logging to a new file everyday.

So, here is how you do it.
1. Tweek the log4j property to use system property as the file to log into.
2. Update the system property every day using a scheduled process.

Lets use a sample to do this.

I’ll be using spring v3 to achieve. If you don’t like spring, you may use some other way of doing this.
NOTE:Along with the following code, please remember to use the vm property, -DlogFile=log.log. This sets the system property logFile which is used by the LogRotator.java to change the file name dynamically every minute.

Conclusion

Using log4j.appender.file.File=${logFile} ensures that the file name comes from a system property ‘logFile’. We are using a cron job using spring and quartz which runs every minute and resets the log configuration and uses the ‘log4j.properties’ file to re-configure logger properties.

In file LogRotator.java the following lines are responsible for updating the appender file.
System.setProperty(PROP_LOG_FILE, PROP_LOG_FILE + “-” + getCurrentDate() + “.log”);
LogManager.resetConfiguration(); // resets the configuration of the logger
PropertyConfigurator.configure(prop); // re configures the logger using the same log4j.properties file

Source Code

Find the source code at: https://github.com/73ddy/logrotate