Rolling Tomcat Logs

Tomcat's log file is called catalina.out. Without setting up rolling logs, this log can get quite large.

This works by installing JULI, which is a log manager specific to Tomcat. JULI is enabled by default in Tomcat, but we must install some extra files to get it to work with log4j.

The following is an example of how log messages work. In this example, let's assume that today is August 13, 2009. You start catalina by running bin/catalina.sh start and log messages are written out to catalina.out. At midnight, all the log messages in catalina.out are moved to catalina.out.2009-08-13 and catalina.out is emptied. The log messages for your current day (August 14, 2009) are written to catalina.out. When midnight comes again, the messages from August 14, 2009 are moved into catalina.out.2009-08-14 and new messages go into catalina.out.

The titles of the logs from previous days will always be in the catalina.out.YYYY-MM-dd format. ie for December 31st, 2008 this would be catalina.out.2008-12-31.

These instructions explain how to set up rolling logs:

  1. Download the stable branch of Log4J (currently 1.2) and place the log4j jar into $CATALINA_HOME/lib.
  2. If you are not sure what version of tomcat you have, navigate to your tomcat installation and run: bin/catalina.sh version It will output a server number, which is your Tomcat version.

3. Go to the Tomcat archives. On the webpage, click on the link for your tomcat version. Then navigate to the bin\extras page and download tomcat-juli.jar and tomcat-juli-adapters.jar.

  1. Replace $CATALINA_HOME/bin/tomcat-juli.jar with the tomcat-juli.jar from step 3. (It might seem strange to replace the jar with a jar of the same name, but the jar from step 3 is not identical).
  2. Place the tomcat-juli-adapters.jar from step 3 in $CATALINA_HOME/lib.
  3. Open $CATALINA_HOME/conf/context.xml. On the line that says <Context> edit it so that it says <Context swallowOutput="true">. This is so that the standard out and standard error are piped into the loggers.
  4. Create a log file called log4j.properties with the following content and save it into $CATALINA_HOME/lib.
    log4j.rootLogger=info, stdout, R
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%p %t %c - %m%n
    log4j.appender.stdout.Target=System.out
    log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.R.DatePattern='.'yyyy-MM-dd
    log4j.appender.R.File=${catalina.home}/logs/catalina.out
    log4j.appender.R.layout=org.apache.log4j.PatternLayout
    log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
    

This creates a logger with logging level "info" that rolls catalina.out every day at midnight. The stdout logger ensures that standard out is also sent to the console, so that you can easily see if there are any errors with running tomcat.

  1. If you find that your logs have duplicate entries, then you should remove the changes you made to $CATALINA_HOME/conf/context.xml in step 6. You should use this log4j.properties file instead:
    log4j.rootLogger=info, R
    log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.R.DatePattern='.'yyyy-MM-dd
    log4j.appender.R.File=${catalina.home}/logs/catalina.out
    log4j.appender.R.layout=org.apache.log4j.PatternLayout
    log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
    

We included these alternate instructions because the issue with duplicate entries happen on some platforms and not others.

These instructions are a modified version of the Tomcat logging instructions.

If you would like to delete old logs, you might want to run a cronjob to search for old logs and delete them.

You can run crontab -e to create a cronjob. There you should enter something like:

0  0  *  *  *  find $CATALINA_HOME/logs -type f -mtime +30 | xargs --no-run-if-empty rm

This will run the job every day at midnight. The job uses find to purge log files 30 days old or older.