Tomcat and Log4J
After battling with several different configurations when it comes to logging and Tomcat 5.5.x, I decided to put together this guide for a simplistic approach. This assumes that you would like to use Log4J for both Tomcat’s internal logging as well as your deployed web application’s logging needs.
For the impatient, here are the steps:
- Place a log4j.properties file which uses the RollingFileAppender in common/classes.
- Add your log4j.jar to the common/lib directory.
- Add commons-logging.jar (1.0.4 or later) and log4j.jar to your application's WEB-INF/lib directory.
- Add a log4j.properties OR log4j.xml file to your application's WEB-INF/classes directory.
As outlined in the Tomcat 5.5 documentation, Logging in Tomcat, add a log4j.properties file to your common/classes directory:
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{MM/dd HH:mm:ss} %-5p %30.30c %x - %m\n
The above configuration is a bit different than the sample given in the Tomcat docs. I’ve changed the log4j.rootLogger to use the INFO logging level instead of DEBUG. I also changed the layout.ConversionPattern to match what I usually use in my application logging. It’s minimal and just enough to let you know Tomcat is happy. If you run into problems during startup or you’re debugging during development, you can jack the rootLogger level up to DEBUG.
You should find a tomcat.log file in the logs directory with a few lines like this after startup:
01/19 11:41:26 INFO yote.http11.Http11BaseProtocol - Initializing Coyote HTTP/1.1 on http-8080 01/19 11:41:26 INFO ache.catalina.startup.Catalina - Initialization processed in 637 ms 01/19 11:41:26 INFO .catalina.core.StandardService - Starting service Catalina 01/19 11:41:26 INFO e.catalina.core.StandardEngine - Starting Servlet Engine: Apache Tomcat/5.5.23 01/19 11:41:27 INFO che.catalina.core.StandardHost - XML validation disabled 01/19 11:41:29 INFO yote.http11.Http11BaseProtocol - Starting Coyote HTTP/1.1 on http-8080 01/19 11:41:29 INFO ache.catalina.startup.Catalina - Server startup in 2288 ms
If you forget to place a log4j.properties file in your common/classes directory you will get this on startup:
log4j:WARN No appenders could be found for logger (org.apache.catalina.startup.Embedded). log4j:WARN Please initialize the log4j system properly.
For my application’s log4j configuration file, I choose XML just as a personal preference. So a typical configuration is something like this:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
<appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MM/dd HH:mm:ss} %-5p %30.30c %x - %m\n"/>
</layout>
</appender>
<appender name="FileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="../logs/builder-log4j.log"/>
<param name="MaxFileSize" value="10MB"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MM/dd HH:mm:ss} %-5p %30.30c %x - %m\n"/>
</layout>
</appender>
<logger name="com.digitalsanctum.builder.web" additivity="false">
<level value="debug"/>
<appender-ref ref="ConsoleAppender"/>
</logger>
<root>
<level value="info"/>
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="FileAppender"/>
</root>
</log4j:configuration>
That’s pretty much it. I suggest spending time when your web application is closer to completion to find a good combination of appenders which gives you enough information to get a good feel of how your application is behaving. I also suggest using a conservative configuration (perhaps the WARN or ERROR level) for your production environment.
blog comments powered by Disqus