Log4j Version 1.3 and Apache Tomcat

Written by Geoff Mottram (geoff at minaret dot biz).

Placed in the public domain on January 27, 2005 by the author.

This document is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the author be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with this document or the use or other dealings in this document.

Contents
Introduction
Tomcat and Log4j 1.3
Log4j Configuration File

Introduction
With the introduction of Log4j version 1.3 (now in alpha testing), it is now possible to configure a daily rolling log file for Tomcat without having to write your own appender (like DatedFileAppender). The only catch is that you cannot configure the new rolling file appender with a properties file but you must use an XML file instead.

For more detailed information on logging with Tomcat and Log4j, please see Logging for Apache Tomcat and Velocity using Log4j. The following instructions have been tested with Apache Tomcat version 5.5 running on Java 5 (a.k.a. 1.5). While this should work with Tomcat 5.0 and older versions of Java, you may need to install an extra jar file or two as the following Log4j configuration requires an XML parser.

Tomcat and Log4j 1.3
When installing or removing Log4j from your Tomcat installation, make sure that you shut down Tomcat first. If you are already using an older version of Log4j, remove it from your Tomcat installation. This means moving any log4j jar and properties files out of the usual places. The jar file is usually installed in the Tomcat common/lib directory and the properties file in common/classes. It is best to move these files into another directory and not delete them (i.e. mv log4j* ..).

To configure Log4j 1.3 as your log manager in Tomcat (skip the first two steps if you have configured a previous version of Log4J as your Tomcat logger):

  1. Download the Commons Logging package from the Apache web site (unless you already have it).
  2. Copy the commons-logging.jar file from the distribution into your Tomcat common/lib directory (unless you have done this before).
  3. Download version 1.3 of the Log4j package from the Apache web site (unless you already have it).
  4. Copy the log4j-1.3alpha-6.jar or similar file from the distribution into your Tomcat common/lib directory.
  5. Create a log4j.xml file in your Tomcat common/classes directory (see next section).

Log4j Configuration File
The configuration described here results in the creation of two log files for Tomcat 5.5 and three log files for Tomcat 5.0: the Servlet log file (only with Tomcat 5.0), which will roll over to a new file every night; the Commons Logging data via log4j (which will also roll over every night) and the catalina.out file, which will only contain messages that have been printed to standard output and standard error. The last file will grow forever but well behaved applications within your Tomcat server should not be printing to standard out or error, so this should not really be an issue.

Unfortunately, Log4j outputs a fair amount of INFO level messages both when it starts up and when it shuts down. In the former case, these messages will end up in your catalina.out file and in the latter case, they will roll onto your console since all internal Log4j messages are sent to standard out or error. You can get even more detail from Log4j by setting the debug="true" attribute of the root Log4j configuration element. It is not possible to stop these INFO level messages without disabling Log4j internal error messages as well.

Your log4j.xml file should look something like this:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration>

<configuration xmlns="http://logging.apache.org/">
<!-- <configuration xmlns="http://logging.apache.org/" debug="true"> -->

  <!-- Create a rolling file appender called "T" for the Tomcat system log -->
  <appender name="T" class="org.apache.log4j.rolling.RollingFileAppender">
    <!-- Log files should use today's date and rotate at midnight -->
    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
      <!-- Set the path and file name pattern for your Tomcat log files -->
      <param name="FileNamePattern" value="/usr/local/tomcat/logs/tomcat.%d.log"/>
    </rollingPolicy>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n"/>
    </layout>
  </appender>

  <!-- Configure the root appender -->
  <root>
    <appender-ref ref="T"/>
     <!-- Set your root logging level: debug, info, warn, error or fatal -->
    <level value="info"/>
  </root>

  <!-- Configure the log level for Struts -->
  <!--
  <logger name="org.apache.struts">
    <level value="debug"/>
  </logger >
  -->

  <!-- Configure the log level for Velocity -->
  <!--
  <logger name="org.apache.velocity.runtime.log.SimpleLog4JLogSystem">
    <level value="warn"/>
  </logger >
  -->
</configuration>
   
The only change you have to make is to edit the line with the FileNamePattern element to point to your Tomcat logs directory.

The above log4j configuration will log info messages as well as those that are more important (warning, error and fatal) to a series of files named tomcat.YYYY-mm-dd.log in your Tomcat logs directory. The first new message that is logged after midnight will cause a new file to be created, using the new date. In production, you will probably want to set the root logging level to warn or error.

At the end of the configuration file you will notice two sections that are commented out. They are an example of how you can configure the logging characteristics for specific applications.

Caveat
Be careful when setting the logging level to debug (particularly at the root level, as an enormous amount of information can be generated, slowing your system down considerably.

Technical Tips