Friday, September 19, 2014

Handling Tomcat Catalina logs with logrotate

Tomcat by default logs on catalina.out file. Tomcat does not have log4j files where we can specify the various parameters to rotate the logs. If left unattended, this can lead to catalina files becoming huge. The big size catalina log files results in
  • Disk space getting chocked
  • Application become slower as when they have to log they have to deal with a large file to work with,
To handle proper rotation of catalina log file, in Linux we can use the logrotate capability. If you
want to use the standard way of using logrotate utility than you can follow the logrotate post here. 

However if you want to do a custom logrotation based on size and want to check for the logs more frequently, than you might want to put a script which can look into the log files on more frequent basis and rotate the logs based on size.

Let's assume that the catalina log file of tomcat is sitting at /var/log/tomcat/catalina.out. Create a logrotate configuration file at a given location. For this example let's assume you create a file with name catalina at /etc/mylogrotate.d/

A sample file might look like

/var/log/tomcat/catalina.out { 
    copytruncate 
    rotate 7 
    compress 
    missingok 
    size 10M 
}

Now make an entry into the crontab file so that logrotate invokes the log rotation logic. Let's say we want to run it every hour and let's assume logrotate is at /usr/sbin/logrotate. The entry in crontab below will run every 10th minute of the each hour. For example it will run at 1:10, 2:10 and so on

10 * * * * /usr/sbin/logrotate /etc/mylogrotate.d/catalina

You an test if the things will run fine by invoking the command on a shell

logrotate /etc/mylogrotate.d/catalina

Hopefully this will enable to handle your ever growing catalina.out file.

No comments:

Post a Comment