Enabling log rotation in Tomcat

This document details the required steps for setting up log rotation in Tomcat running on CentOS 5, when it is installed from the Loway repository.

Prerequisites

  • A working QueueMetrics instance, installed via yum

The rotation file

If you would like to setup Tomcat logrotation you can do the following:

vi /etc/logrotate.d/qm-tomcat6

This will create the logrotate file, that you will have to set up as follows:

/usr/local/queuemetrics/tomcat/logs/*.log {
  notifempty
  copytruncate
  daily
  rotate 10
  compress
  missingok
}

/usr/local/queuemetrics/tomcat/logs/catalina.out {
  notifempty
  copytruncate
  dateext
  daily
  rotate 10
  compress
  missingok
}

If you want to, you can make it take care of cleaning out temp files as well (or just do it from cron as you would normally)

/usr/local/queuemetrics/tomcat/logs/catalina.out {
  notifempty
  copytruncate
  dateext
  daily
  rotate 10
  compress
  missingok
  postrotate
    /bin/nice /usr/bin/find /usr/local/queuemetrics/tomcat/temp -type f -mtime +10 -exec /bin/rm {} \; > /dev/null
  endscript
}

Misc changes

You can also look into the CentOS logrotation settings and implement some of the following changes, depending on your needs. Even if you set the rotation to daily in qm-tomcat6, logrotate.conf overrides it to weekly.

vi /etc/logrotate.conf

Set weekly to daily

# rotate log files daily
daily

Change the backlogs from 4 week to 14 days

# keep 14 days worth of backlogs
rotate 14

Compressed logs

# uncomment this if you want your log files compressed
compress

By default logrotation runs at 4am. You can change this by editing /etc/crontab

Set it to 02:02

02 2 * * * root run-parts /etc/cron.daily

Changes to QueueMetrics

None required.