How to set up log rotation for Tomcat catalina.out log file

If catalina.out grows larger than 2GB it can cause Tomcat to crash, which is a bad thing. To avoid this you can set up log rotation.

Create a new config file:

$ sudo vi /etc/logrotate.d/tomcat

Then specify the details of the rotation:

/var/tomcat/log/catalina.out {
	copytruncate
	daily
	dateext
	rotate 14
	compress
	missingok
	size 20M
	}

What did we do here?

  • Specify the path to the log file.
  • copytruncate – Creates a copy of the log file and then truncates the original to an empty file so that the service can keep on logging uninterrupted.
  • daily – Performs the truncation once a day.
  • dateext – Add the date to the filename of the archived log file.
  • rotate 14 – Keep only 14 rotated log files.
  • compress – Create a gzip compressed file of the old log.
  • missingok – Suppresses error messages if the file does not exist.
  • size 20M – Logrotate will only run when the file is 20MB or more in size.

How does this run?

On most Linux distros you will find /etc/cron.daily/logrotate, which will run daily. It executes /usr/sbin/logrotate etc/logrotate.conf, which in turn will run the Tomcat rotation file you have added to /etc/logrotate.d/. If you run multiple Tomcat instances, create a separate rotation file for each instance.

If you do not have logrotation configured and there is a desperate need to quickly clear down a giant log file on a non-production system, and you don’t care about losing the current info in the log, try

$ > /var/tomcat/log/catalina.out

This writes null to the file, i.e. clears it out completely. Usually Tomcat will happily continue to log to the file, but you definitely do not want to do this on a production machine!

Be Sociable, Share!

No related posts.