Monday, November 3, 2008

Logrotate and log4j

I discovered the power of linux logroate today. A simple config file and you get automatic rotation + retaintion policy for your application logs. Take a look...

http://linuxcommand.org/man_pages/logrotate8.html

Things get interesting when you want to use this one with log4j. log4j has DailyFileAppender

org.apache.log4j.DailyFileAppender

This is the one everybody ends up using, But the trouble here is you can never configure it correctly to work with logroatate. Nor should you try, It's like two programs are trying to do the same thing...

The closest thing to logroate in log4j is
org.apache.log4j.RollingFileAppender

you can configure it to a very high max size (say 1 GB) and set Maxbackupindex to zero ! . It is kind of an way of using it as a FileAppender with a max cap on size. After this it's logroate's job to take care of rotation and retaintion.

Typical log4j Appender
*(log4j.xml)
<appender name="MyAppender" class="org.apache.log4j.RollingFileAppender">
<param name="MaxFileSize" value="1GB" />
<param name="MaxBackupIndex" value="0" />
<param name="File" value="/path/to/log" />
....
</appender>
(myApp_logroate.conf)

Retain 4 weeks worth of log.

"/path/to/log" {
missingok
notifempty
copytruncate
compress
size 100M
weekly
rotate 4
}

2 comments:

Carsten Dumke said...

log4j 1.2 contains a class org.apache.log4j.FileAppender which does only grow the logfile and leaves the rotate-task to logrotate.

My Tomcat-log4j-configuration uses this appender and looks like this:

log4j.appender.CATALINA=org.apache.log4j.FileAppender
log4j.appender.CATALINA.File=${catalina.base}/logs/catalina.log
log4j.appender.CATALINA.Append=true
log4j.appender.CATALINA.Encoding=UTF-8
log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout
log4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

Best regards,
Carsten

Carsten Dumke said...

My logrotate-configuration for Tomcat looks like:

/var/log/tomcat6/catalina.log {
copytruncate
daily
rotate 30
compress
missingok
create 0644 tomcat tomcat
}

The essential option is copytruncate.

Best regards,
Carsten