Tomcat 6 with logrotate

0 Comments

Quite a few of my clients purport to being professional Java developers. The problem I have with such a statement is that they always use Tomcat and never know how to set it up. Now, I am not saying that they are not professionals. I am saying that I try and understand the tools with which I make a living and most of the people I have trained are the same way. Anyway, the number one problem I always get asked to fix is log rotation.

As we all know, Tomcat loves date stamping the log files by default. We also know that logrotate cannot work with such filenames very well. This is something I have never liked about Tomcat and it is not going to suddenly change any time in the foreseeable future. This fix, thank goodness, is simple. Seeing as how Debian is the only production platform I tolerate in my Department I will be basing all configs on a basic Debian install using the standard Tomcat 6 package.

Basically you have 2 files that need to be changed. The first is /etc/tomcat6/server.xml. As you can see below, the original config creates the access_log.SOME_DATE.txt file (where SOME_DATE is the date stamp). This is great for Developers but crap for production systems because a new log is created every day while the old logs are not compressed and just sit there taking up space until Nagios/Icinga throw a warning one day that the drive is full.

Original

To get a normal file called “access.log” that logrotate can actually rotate and track correctly you will need something like the following:

Modified

The main point to catch is the “rotatable” setting which will drop the date stamp. You will also want to remove the “.” from the end of the prefix or the beginning of the suffix.

The next file to be modified is /etc/tomcat6/logging.properties

Original
1catalina.org.apache.juli.FileHandler.level = FINE
1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
1catalina.org.apache.juli.FileHandler.prefix = catalina.

Modified
1catalina.org.apache.juli.FileHandler.level = FINE
1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
1catalina.org.apache.juli.FileHandler.prefix = catalina
1catalina.org.apache.juli.FileHandler.rotatable = false

Once again notice the “rotatable” and the missing “.” on the end of the prefix.

Afterwards all you have to do is add the following to /etc/logrotate.d/tomcat6

/var/log/tomcat6/catalina.out {
copytruncate
weekly
rotate 52
compress
missingok
create 640 tomcat6 adm
}

/var/log/tomcat6/catalina.log {
copytruncate
weekly
rotate 52
compress
missingok
create 640 tomcat6 adm
}

/var/log/tomcat6/access.log {
copytruncate
weekly
rotate 52
compress
missingok
create 640 tomcat6 adm
}

Everything working fine now and you can get rid of the uncompressed log files as soon as you are able to.

Tags: , , ,

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.