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 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
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
|
Levels | This determines at what level the information should be, the below is order of verbosity
There are two special levels as well
|
Filter | You can filter specific log messages, they can be used to allow or deny log messages that match a string pattern
|
Layouts | This determines how the logging format should be laid out, mostly you will use the SimpleLayout option, but there is a HTML option.
|
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 |
Specific Package or Class | log4j.logger.mypackage = WARN, myConsoleAppender log4j.logger.mypackage.LoggingExample = WARN, myConsoleAppender |
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
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
|
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
Like the log4j there are two special ones
|
Formatter | There are two formatters in JULI
|
Example |
|
# Set Handlers # Set the level for the 'root logger' 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 |
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.