Tomcat Logging

Tomcat 6 now uses a new Java logging method, it also logs per JVM and not per class loader. To overcome this limitation they implemented Java logging called JULI. This implementation allows you to have different logging property files for each Web application, the property file is conf/logging.properties.

The other logging you can use is log4j which is an open-source Java framework for logging, it can be configured easily to log messages at runtime without greatly affecting the performance of the application. It too uses a configuration file to control the behavior.

log4j

log4j uses a modular design which allows you to change the behavior by changing the configuration file. To setup log4j logging you need to do the following

  1. Download and install log4j.jar (see Apache log4j instructions)
  2. Ensure that the log4j.jar file is in the CLASSPATH (you can put in the lib directopry so all application can use it)
  3. Create a log4j.properties file and make sure that is in the CLASSPATH (normally place in the bin directory)
  4. Add your log statements to your application.

Log4j can be configured a number of ways

Log4j uses a number of components which are used to log the state of the application

Logger this component controls the scope of the logging. You can have more than one logger, loggers like in a hierarchy with rootlogger at the top. Individual classes and packages can have there own loggers too. An appender must be assigned to a logger and its the appender which determines where the logger will be logging too.
Appender

The Appender is what determines where to logging information is to be sent. There are a number of destinations

  • ConsoleAppender
  • FileAppender
  • SMTPAppender
  • JDBCAppender
  • JMSAppender
  • NTEventLogAppender
  • SyslogAppender
  • L5Appender
  • TelnetAppender
  • SocketAppender
Levels

This determines at what level the information should be, the below is order of verbosity

  • TRACE
  • DEBUG
  • INFO
  • WARN
  • ERROR
  • FATAL

There are two special levels as well

  • OFF
  • ALL
Filter

You can filter specific log messages, they can be used to allow or deny log messages that match a string pattern

  • LevelMatchFilter
  • LevelRangeFilter
  • StringMatchFilter
  • DenyAllFilter (stops all log messages)
Layouts

This determines how the logging format should be laid out, mostly you will use the SimpleLayout option, but there is a HTML option.

  • org.apache.log4j.SimpleLayout
  • org.apache.log4j.HTMLLayout
  • org.apache.log4j.PatternLayout
Example
Complete example log4j.datadiskLogger = DEBUG, datadiskSimpleAppender, datadiskConsoleAppender

log4j.appender.datadiskSimpleAppender = org.apache.log4j.FileAppender
log4j.appender.datadiskSimpleAppender.File = c:\datadiskSimpleLog.log
log4j.appender.datadiskSimpleAppender.layout = org.apache.log4j.SimpleLayout

log4j.appender.datadiskConsoleAppender = org.apache.log4j.ConsoleAppender
log4j.appender.datadiskConsoleAppender.layout = org.apache.log4j.SimpleLayout
log4j.appender.datadiskConsoleAppender.Threshold = WARN
log4j.appender.datadiskConsoleAppender.ImmediateFlush = true
Rolling files by date

log4j.datadiskLogger = DEBUG, datadiskSimpleAppender

log4j.appender.datadiskSimpleAppender = org.apache.log4j.FileAppender
log4j.appender.datadiskSimpleAppender.File = c:\datadiskSimpleLog.log
log4j.appender.datadiskSimpleAppender.layout = org.apache.log4j.SimpleLayout
log4j.appender.datadiskSimpleAppender.Threshold = ALL
log4j.appender.datadiskSimpleAppender.Append = true
log4j.appender.datadiskSimpleAppender.DataPattern = '.'yyyy-MM-dd

Specific Package or Class log4j.logger.mypackage = WARN, myConsoleAppender

log4j.logger.mypackage.LoggingExample = WARN, myConsoleAppender

JULI

JULI is Tomcats "container friendly" implementation of Java logging, this logger allows you to support class loader logging. To setup JULI logging you don't need any additonal files, the tomcat-juli-jar file is already bundled with Tomcat

  1. Create log property file and pass it to your application using the java.util.logging.config.file system property, or programmatically. You could even add configuration into the conf/logging.properties file.
  2. Add log statements to your application.

JULI can be configured a number of ways

JULI logging has five components

Logger These are similar to log4j loggers
Handler

Handlers are Appenders in log4j

  • ConsoleHandler
  • FileHandler
  • SocketHandler
  • SMTPHandler
Filter Filters allow a fine degree of control over what is logged than log levels do, there is no default filters but can be used to develop custom ones.
Level

There are seven levels of logging, the list below is in order

  • FINEST
  • FINER
  • FINE
  • CONFIG
  • INFO
  • WARNING
  • SEVERE

Like the log4j there are two special ones

  • OFF
  • ALL
Formatter

There are two formatters in JULI

  • SimpleFormatter
  • XMLFormatter
Example
 

# Set Handlers
handlers = java.util.logging.ConsoleHandler

# Set Handler for the 'root logger'
.handlers = java.util.logging.ConsoleHandler

# Set the level for the 'root logger'
.level = ALL

# Handler specific properties
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.FileHandler.pattern = %t/java%u.log
java.util.logging.FileHandler.level = WARNING
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter

# Set the myPackage logger level
myPackage.level = FINE

Note: below is the key to the pattern used, there are others, there are other attributes for the Filehandler, so check out the documentation.

%t = /tmp or C:\tmp
%u = unique number

Log File Analysis

Sometimes wading through logs can be tiresome, and spotting trends can be impossible, this is where a log analyzer can help and there are number of them on the market, so I would advise you to set one up.