Friday, January 22, 2010

Tomcat 6 Logging

While the official Tomcat documentation states a clear departure from log4j and outlines the possibilities to still use it, I found myself looking for the quickest way to provide instructions to my team as to how to develop using tomcat.

We currently use JBoss in our servers but given the lack of J2EE in our current applications (Spring Framework based) it does not make sense to be waiting for 1 minute JBoss startup when tomcat offers two seconds response.

First when debugging locally it makes sense to see the server output in the console directly. True this can be done using the tail command and that is the way I prefer. Anyway do the below if you want to keep an eye on the server as soon as it is started

#exec "$PRGDIR"/"$EXECUTABLE" start "$@"
exec "$PRGDIR"/"$EXECUTABLE" run "$@"
Now let us make tomcat output our application traces. For that create or edit file at lib/log4j.properties

#### Use two appenders, one to log to console, another to log to a file
log4j.rootCategory=debug, stdout, R

#writes to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %5p [%t] (%F%L) - %m%n

#writes to a file. This is optional of course in a development environment
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${catalina.base}/logs/stdout.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d %p %t %c - %m%n
#Archive logs
log4j.appender.R.MaxBackupIndex=10
#maximum log file size
log4j.appender.R.MaxFileSize=10000KB


From logs/stdout.log you can get all information you want and of course the same applies for the command line from where you started the server.

Be aware that your application must not have any log4j.properties file in the classpath otherwise you will end up overwriting the server configuration.

No comments:

Followers