Logging (Log4J/Jakarta Commons) Config
The Jakarta Commons Logging package is an ultra-thin bridge between different logging libraries. Components may use the Commons Logging API to remove compile-time and run-time dependencies on any particular logging package, and contributors may write Log implementations for the library of their choice.
Levels To Use
The six logging levels provided by the commons logging API and my suggested uses for them are (in order):
- trace the least serious; not to be used unless very fine grained tracing is required in a complex piece of code.
- debug used to print debugging information, helpful in development and for debugging; this will almost certainly be disabled when running in production.
- info informatory messages to the logging destination, which help understanding the flow of control within an application; significant application events should be logged at this level (account creation, account closed, etc.).
- warn information related to some faulty and unexpected behavior of the system, which needs attention in near future or else can cause malfunctioning of the application.
- error error related messages; this will very likely be configured to email the associated error message and exception to the operations/support team and should be used sparingly.
- fatal system critical information, for problems which are causing the application to crash; not to be used.
Utilizing the commons api for logging allows for a simple migration to any of the supported logging implementation with little or no code change. Logging messages within your code is simple:
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class SomeClass {
private static final transient Log log = LogFactory.getLog(SomeClass.class);
...
public void someMethod() {
log.info("This is the log message");
... do something ...
}
public void someOtherMethod() {
if (log.isDebugEnabled()) {
... do something complex...
log .debug(theResult);
}
}
}
Log4J Implementation
In order to activate the log4j functionality within tomcat you must tell tomcat how to use log4j.Ensure that the log4j.jar is available in TOMCAT_HOME/common/lib and copy the log4j.properties.sample file to TOMCAT_HOME/common/classes/log4j.properties. Log4J only supports one instance per container so we configure the logging properties at the container level so that the individual webapps do not interfere with eachother.
Sample Log4J Properties File
###############################################################################
# Set default logging level and default appenders.
#
# Syntax: log4j.[logger]=[level], appenderName, appenderName, ...
###############################################################################
log4j.rootLogger=INFO, console, file
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${catalina.home}/logs/stdout.log
log4j.appender.file.MaxFileSize=512KB
log4j.appender.file.MaxBackupIndex=3
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %p [%c] - %m%
### We don't want items outside of com.neteller to log to our custom
### log files so set additivity to false.
log4j.additivity.com.xxx=false
###############################################################################
# Backend classes
###############################################################################
### Spring
#log4j.logger.org.springframework=WARN
### Hibernate
#log4j.logger.net.sf.hibernate=INFO
#log4j.logger.net.sf.hibernate.SQL=DEBUG
### EHCache
#net.sf.hibernate.cache=DEBUG
###############################################################################
# UI classes
###############################################################################
### MyFaces
#log4j.logger.org.apache.myfaces.el.VariableResolverImpl=info
#log4j.logger.org.apache.myfaces=WARN
### Tiles logging
#log4j.logger.org.apache=info
###############################################################################
# Configure member (com.xxx.*) specific logger.
###############################################################################
### Direct log message to the member specific log file as well as to the
### default console (as we defined in tomcat's log4j properties file).
log4j.logger.com.xxx.yyy=DEBUG, console, yyylog
log4j.appender.yyylog=org.apache.log4j.RollingFileAppender
log4j.appender.yyylog.File=${catalina.home}/logs/member.log
log4j.appender.yyylog.MaxFileSize=512KB
log4j.appender.yyylog.MaxBackupIndex=3
log4j.appender.yyylog.layout=org.apache.log4j.PatternLayout
log4j.appender.yyylog.layout.ConversionPattern=%d %p [%c] - %m%n
### Override specific categories to the appropriate logging level here.
#log4j.logger.com.yyylog.service=debug