Tomcat logs its output in a file called catalina.out. Such file contains the output from the server itself and from all the web applications that don't specify an alternative output file.

Different loggers can be used to log web application outputs. The following instructions are for java.util.logging and Log4j.

Web Application logging with java.util.logging

All you have to do is create a file called logging.properties in WEB-INF/classes with the following content, where you have replaced APPLICATION_OUTPUT_FILE with your choice of output file name (it would be nice to have in it the name of the application that produced the file):

handlers = org.apache.juli.FileHandler

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

org.apache.juli.FileHandler.level = FINE
org.apache.juli.FileHandler.directory = ${catalina.base}/logs
org.apache.juli.FileHandler.prefix = APPLICATION_OUTPUT_FILE.

If you now redeploy your application all the output from your application will be logged in a file called

${APPLICATION_OUTPUT_FILE}-${date}.log

so a file is created each day. There is no way to change this configuration as it is all done internally in tomcat.

The advantage of using java.util.logging over log4j is that no additional dependencies are introduced.

Web Application logging with Log4j

To use Log4j for your logging you need to add the following dependencies to your project:

<dependency>
   <groupId>commons-logging</groupId>
   <artifactId>commons-logging</artifactId>
   <version>1.1</version>
   <type>jar</type>
   <url>http://jakarta.apache.org/commons/logging</url>
   <properties>
      <war.bundle>true</war.bundle>
   </properties>
</dependency>
<dependency>
   <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
   <version>1.2.13</version>
   <type>jar</type>
   <url>http://logging.apache.org/log4j/docs/</url>
   <properties>
      <war.bundle>true</war.bundle>
   </properties>
</dependency>

Logging is configured via a properties file (called log4j.properties) that you have to create in WEB-INF/classes. With the following properties file the output is logged in a file called

${APPLICATION_OUTPUT_FILE}.log

The output file is backed up once it reaches 0.5 Mb in size and a maximum of 4 backups are kept.

#
# Configure the logger to output info level messages into a rolling log file.
#
log4j.rootLogger=INFO, R

#
# Default output file name
#
application.context=tomcat

#
# Configuration for a rolling log file.
#
log4j.appender.R=org.apache.log4j.RollingFileAppender
#
# Edit the next line to point to your logs directory.
# The last part of the name is the log file name.
#
log4j.appender.R.File=${catalina.base}/logs/APPLICATION_OUTPUT_FILE.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.MaxFileSize=5036KB
log4j.appender.R.MaxBackupIndex=4

#
# Print the date in ISO 8601 format
#
log4j.appender.R.layout.ConversionPattern=%d %-5p - %m%n

For more configuration options please refer to the log4j documentation at http://logging.apache.org/log4j/docs/.

Logj4 offers more flexibility than java.util.logging but requires additional dependencies.

Redirection stdout and stderr to Logger

By default Tomcat will not redirect stdout and stderr to the Logger. This behaviour ca be changed by setting the context parameter swallowOutput to true. This has been done on glast-tomcat02 and glast-tomcat03.

Monitoring the log file

Use probe to monitor the log file produced by your application going to:

http://_serverName_.slac.stanford.edu:8080/probe/logs/

where serverName is the tomcat server on which the application has been deployed.

Using Logger

With the above steps all the output coming from a web application is redirected to the output file. In general it is better to send messages to the Logger rather than to System.out or System.err. Using Logger it is possible to control the importance level of a message and via the above configuration files it is possible to control which levels are actually logged.

To use a Logger in your java code you have to create one; it is generally a static object in your project:

public static Logger logger = Logger.getLogger("myApplicationLogger");

To log a message at INFO level:

logger.log(Level.INFO,"My message");

The levels can be:

  • SEVERE (highest value)
  • WARNING
  • INFO
  • CONFIG
  • FINE
  • FINER
  • FINEST (lowest value)
  • No labels