How to configure Tomcat 5.0.x to use Java Logging

If you are using java logging and a version of tomcat previous to tomcat 5.5, you have to use the deprecated Logger declarations in your server.xml or
context.xml files. The are deprecated in tomcat 5.0 and have actually been removed in tomcat 5.5.

This is used to achieve separate log files for separate virtual hosts/web applications for example in your server xml you may have…

<Host name="myapp.mydomain.com" debug="0" appBase="webapps/myapp"<br />
      unpackWARs="true" autoDeploy="true"<br />
      xmlValidation="false" xmlNamespaceAware="false">
      <Logger className="org.apache.catalina.logger.FileLogger"<br />
              directory="logs"  prefix="myapp." suffix=".log"<br />
              timestamp="true"/>

This will log the myapp.timestamp.log file with all the contents of the webapp “myapp”.

If you are using log4j you can simply omit this and use the log4j.properties (or log4j.xml file) within your webapp to specify what the logging levels are and where
to log to etc.

Java logging lacks this functionality. The java logging configuration file logging.properies is a jvm-wide properties file, and therefore traditionally
you would have to use the Logger feature to get separate log file per webapp.

In tomcat 5.5 apache introduced juli which is an implementation of the java logger that addresses the shortcomings of the logging.properties file.
i.e. with tomcat 5.5 and above you can have a logging.properties file within your webapp.

Now if you are tied to tomcat 5.0 for your deploys you can actually add juli logging to tomcat 5.0 quite easily, here’s what you do…

Firstly download the tomcat 5.5 core binary distribution. (from http://tomcat.apache.org/download-55.cgi)
From this apache-tomcat-5.5.25.zip file extract the following two files…

apache-tomcat-5.5.25/bin/tomcat-juli.jar into jakarta-tomcat-5.0.28/bin
and
apache-tomcat-5.5.25/conf/logging.properties into jakarta-tomcat-5.0.28/conf

Next we need to edit our catalina startup script to tell java logging about the juli logging jar and properties file.

if you are using a flavour of linux/unix edit jakarta-tomcat-5.0.28/bin/catalina.sh

and under the cygwin section add the following…


# Set juli LogManager if it is present
if [ -r "$CATALINA_BASE"/conf/logging.properties ]; then
 JAVA_OPTS="$JAVA_OPTS "-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager" "-Djava.util.logging.config.file="$CATALINA_BASE/conf/logging.properties"
fi

and then after the classpath section add…


if [ -r "$CATALINA_HOME"/bin/tomcat-juli.jar ]; then
 CLASSPATH="$CLASSPATH":"$CATALINA_HOME"/bin/tomcat-juli.jar
fi

if you are using windows edit jakarta-tomcat-5.0.28/bin/catalina.bat

After the setenv.bat section add…


if not exist "%CATALINA_BASE%conflogging.properties" goto noJuliProps
set JAVA_OPTS=%JAVA_OPTS% -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file="%CATALINA_BASE%conflogging.properties"
:noJuliProps

and then after the classpath section add…


if not exist "%CATALINA_HOME%bintomcat-juli.jar" goto noJuli
set CLASSPATH=%CLASSPATH%;%CATALINA_HOME%bintomcat-juli.jar
:noJuli

And voila that’s it!

All you need now in your webapp is add your own specific logging.properties file to the WEB-INF/classes directory of the war, an example of which….


####### --------- logging.properties --------- #############
handlers = org.apache.juli.FileHandler, java.util.logging.ConsoleHandler

############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################

org.apache.juli.FileHandler.level=FINEST
org.apache.juli.FileHandler.directory=/var/log/tomcat
org.apache.juli.FileHandler.prefix=myapp.

java.util.logging.ConsoleHandler.level=FINEST
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter

Technorati Tags: , , , ,