Thursday, November 06, 2008

TOMCAT LOGS: Rotating catalina.out on a solaris box

I tried several different solutions for getting a rotated log on tomcat. I tried configuring a new tomcat.log and putting log4j and commonslog jars inside common/lib, but that just created a new log. When I thought I would have to live with the endless catalina.out, I tried logadm, which with a few configurations actually worked just as I wanted!!!

So here it goes.

1) In a directory which the user you are running tomcat can write create a file called logadm.conf with the following content (assuming tomcat is in the /opt/tomcat/ folder):

/opt/tomcat/logs/catalina.out -C 15 -c -t '/opt/tomcat/logs/$basename.%Y-%m-%d'

2) Create a cron job for the same user (assuming you saved the configuration in /opt/tomcat/logrotate):

You can add the cron entry to the user that is running tomcat in the following way:

export EDITOR=vi
crontab -e

add the line:

0 0 * * * logadm -f /opt/tomcat/logrotate/logadm.conf

This will make it run every midnight of everyday.
  • Explanation on the parameters of logadm

This command, that exists since the solaris 9, will rotate the catalina.out, will rotate the specified log file. The -C tells how many old copies of the files will be kept. The -c is the secret here. It will truncate the log file instead of renaming. This way tomcat keeps the handle to the file and can keep logging. -t uses a template to create the rotated files. Without that it will just append a .1, .2, etc.

You should run it manually before adding to your crontab.

That should be all!!!!

3 comments:

roko said...

¡ obrigado !

Thansk a lot for this information, I didn't know "logadm" existance in solaris, now I think this will save my life.

Fadli Maizar said...

1) In a directory which the user you are running tomcat can write create a file called logadm.conf with the following content (assuming tomcat is in the /opt/tomcat/ folder):

/opt/tomcat/logs/catalina.out -C 15 -c -t '/opt/tomcat/logs/$basename.%Y-%m-%d'

if I want to put log rotated
can I change '/opt/tomcat/logs/$basename.%Y-%m-%d' become '/data/logs/$basename.%Y-%m-%d'

because my Tomcat_HOMe less fre space

Mehrdad said...

Hi,
What do you mean with $basename? Is my assumption right:
basename=catalina_rotate
Kind regards
Mehrdad