Tuesday, March 17, 2009

Roll catalina.out in Tomcat 5.5

A few great solutions to the problem of catalina.out getting too big are to use chronolog or rotatelogs. While tempting, redirecting to /dev/null is not a good idea, because there are things that are/can be printed to console that aren't logged (log4j config errors, garbage collection stats, possibly runtime errors, init -3 dumps, other messages and stacktraces printed directly to console).

Camelrichard.org discusses a solution to this issue using chronolog at:
* http://www.camelrichard.org/rotating-catalina-out-tomcat-5-5-using-cronolog

His solution is to make the following changes to catalina.sh:

1) Remove:

touch "$CATALINA_BASE"/logs/catalina.out
2) Change:
org.apache.catalina.startup.Bootstrap "$@" start \
>> "$CATALINA_BASE"/logs/catalina.out 2>&1 &
to:
org.apache.catalina.startup.Bootstrap "$@" start 2>&1 \
| /path/to/cronolog "$CATALINA_BASE"/logs/catalina.out.%Y-%m-%d >> /dev/null &

By the same token, you could use rotatelogs if you wanted using a solution similar to Camelrichard's, but note that it won't create the log directory if it doesn't exist (at least in the version we use). For example:

1) Remove:

touch "$CATALINA_BASE"/logs/catalina.out
2) Change:
org.apache.catalina.startup.Bootstrap "$@" start \
>> "$CATALINA_BASE"/logs/catalina.out 2>&1 &
to:
org.apache.catalina.startup.Bootstrap "$@" start 2>&1 \
| /usr/sbin/rotatelogs "$CATALINA_BASE"/logs/catalina.out.%Y-%m-%d 86400 >> /dev/null &

At time of writing neither cronolog nor rotatelogs have the ability to only keep a certain number of logs or keep them for a certain number of days. You'll need to find another solution for that (like a cronned process for removing logs or archiving them).

Although it might be tempting, you will not want to redirect to /dev/null like the following, because you'll lose data as stated above:

1) Remove:

touch "$CATALINA_BASE"/logs/catalina.out
2) Change:
org.apache.catalina.startup.Bootstrap "$@" start \
>> "$CATALINA_BASE"/logs/catalina.out 2>&1 &
to:
org.apache.catalina.startup.Bootstrap "$@" start \
>> /dev/null 2>&1 &

In any event, see the Tomcat 5.5 logging documentation. Here is that same information with the config changed a little, because you will want the timestamp in the logs and usually you will want INFO instead of DEBUG as in their example. You should make sure that the pattern contains all the info you want to log:

1. Create a file called log4j.properties with the following content and save it into common/classes:

log4j.rootLogger=INFO, R 
log4j.appender.R=org.apache.log4j.RollingFileAppender 
log4j.appender.R.File=${catalina.home}/logs/tomcat.log 
log4j.appender.R.MaxFileSize=10MB 
log4j.appender.R.MaxBackupIndex=10 
log4j.appender.R.layout=org.apache.log4j.PatternLayout 
log4j.appender.R.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

2. Download Log4J (v1.2 or later) and place the log4j jar in $CATALINA_HOME/common/lib.

3. Download Commons Logging and place the commons-logging-x.y.z.jar (not commons-logging-api-x.y.z.jar) in $CATALINA_HOME/common/lib with the log4j jar.

4. Start Tomcat

Hope that helps.

No comments: