Search

Dark theme | Light theme

February 9, 2011

Configure Log4j on Tomcat

How-to configure Log4j on Tomcat is described in the Tomcat documentation, but the documentation sample configuration is not correct. So in this post we see what we need to do to replace the default Java Util Logging (JUL) configuration and use Log4j in Tomcat. A big advantage is that if an application uses Log4j then we can configure the logging at a central place for the Tomcat instance. And another advantage is we can use our knowledge on how to configure Log4j logging and we don't have to learn Tomcat's syntax extensions to the JUL logging configuration.

Setup the classpath

First we must add same extra JAR files to our Tomcat classpath. We replace the default JUL Tomcat library with one that support Log4j. And of course we must add the Log4j library to the classpath.

Tomcat already provides the JAR files we need to use Log4j in Tomcat. These files are not in the normal distribution of Tomcat, but we can download them from the website. On the download page we must select the Browse link. From there we go to bin/extras and here we see two files we must download: tomcat-juli.jar and tomcat-juli-adapters.jar. We place the tomcat-juli.jar file in our $CATALINA_BASE/bin directory. The file tomcat-juli-adapters.jar is copied to our $CATALINA_BASE/lib directory.

Next we download the latest log4j 1.2 library from the download page. We must unpack the downloaded file to a directory on our computer. Next we copy from the directory with the extracted file we downloaded the file log4j-1.2.<version>.jar to the $CATALIN_BASE/lib directory.

Add Log4j configuration

Our classpath is now setup and we can add our Log4j configuration file. First we disable the old Tomcat JUL logging configuration. We find this configuration file is in the $CATALINA_BASE/conf directory. The name of the file is logging.properties and we move this file to $CATALINA_BASE/conf/logging.properties.jul. Tomcat cannot use the file for configuration, but we still have a backup. With the old configuration out of the way we can create a new Log4j configuration file. In the $CATALINA_BASE/lib directory we create the file log4j.properties. If we look at the sample configuration file in the Tomcat documentation we notice the file contains errors. For example the conversionPattern is not configured on the layout property. We can use the following sample Log4j configuration:

log4j.debug=true
log4j.rootLogger=INFO, CATALINA, CONSOLE

# Define all the appenders
log4j.appender.CATALINA=org.apache.log4j.FileAppender
log4j.appender.CATALINA.file=${catalina.base}/logs/catalina.log
log4j.appender.CATALINA.encoding=UTF-8
log4j.appender.CATALINA.layout=org.apache.log4j.PatternLayout
log4j.appender.CATALINA.layout.conversionPattern = %d [%t] %-5p %c - %m%n
log4j.appender.CATALINA.append=true

log4j.appender.LOCALHOST=org.apache.log4j.FileAppender
log4j.appender.LOCALHOST.file=${catalina.base}/logs/localhost.log
log4j.appender.LOCALHOST.encoding=UTF-8
log4j.appender.LOCALHOST.layout=org.apache.log4j.PatternLayout
log4j.appender.LOCALHOST.layout.conversionPattern = %d [%t] %-5p %c - %m%n
log4j.appender.LOCALHOST.append=true

log4j.appender.MANAGER=org.apache.log4j.FileAppender
log4j.appender.MANAGER.file=${catalina.base}/logs/manager.log
log4j.appender.MANAGER.encoding=UTF-8
log4j.appender.MANAGER.layout=org.apache.log4j.PatternLayout
log4j.appender.MANAGER.layout.conversionPattern = %d [%t] %-5p %c - %m%n
log4j.appender.MANAGER.append=true

log4j.appender.HOST-MANAGER=org.apache.log4j.FileAppender
log4j.appender.HOST-MANAGER.file=${catalina.base}/logs/host-manager.log
log4j.appender.HOST-MANAGER.encoding=UTF-8
log4j.appender.HOST-MANAGER.layout=org.apache.log4j.PatternLayout
log4j.appender.HOST-MANAGER.layout.conversionPattern = %d [%t] %-5p %c - %m%n
log4j.appender.HOST-MANAGER.append=true

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.encoding=UTF-8
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.conversionPattern=%d [%t] %-5p %c - %m%n

# Configure which loggers log to which appenders
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=INFO, LOCALHOST
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]=\
  INFO, MANAGER
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]=\
  INFO, HOST-MANAGER

We are ready and we can start Tomcat. Tomcat starts up and uses Log4j for logging messages.

Everything works fine, but if we don't want to the Log4j configuration in the $CATALINA_BASE/lib directory but in the $CATALINA_BASE/conf directory? We can move the log4j.properties file from the $CATALINA_BASE/lib directory to the $CATALINA_BASE/conf directory. Then we must use the system property -Dlog4j.configuration=file://$CATALINA_BASE/conf/log4j.properties when we start Tomcat. In Tomcat 6 and 7 we can use the environment variable LOGGING_CONFIG to set this value, because it is used by the Catalina scripts when we start Tomcat.