|
Recipe 13.2. Configuring Struts LoggingProblemYou need to set the severity level and details of the log messages generated by internal Struts components. SolutionConfigure Struts logging to use Log4J as its logging implementation:
Example 13-1. Configuring Log4J for Struts packages# The output information consists of relative time, log level, thread # name, logger name, nested diagnostic context and the message in that # order. # For the general syntax of property based configuration files see the # documentation of org.apache.log4j.PropertyConfigurator. log4j.rootLogger=WARN,Console log4j.logger.org.apache.struts=DEBUG,Console,File log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p %c - %m%n # File is set to be a FileAppender which outputs to struts-debug.log log4j.appender.File=org.apache.log4j.FileAppender log4j.appender.File.file=struts-log.log # File uses PatternLayout. log4j.appender.File.layout=org.apache.log4j.PatternLayout # The conversion pattern uses format specifiers. You might want to # change the pattern an watch the output format change. log4j.appender.File.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
Example 13-2. Configuring the Commons Logging factory# Set the commons logging factory to the Log4J implementation org.apache.commons.logging.LogFactory=org.apache.commons. logging.impl.Log4jFactory DiscussionIf your application is misbehaving, the first thing to do is check the log files. If Struts is recording informational messages, then you will need to adjust the logging so you can see more detail. Struts records log messages using the Jakarta Commons Logging API. This API acts as a lightweight, generic façade around several popular Java logging packages. Most users employ the Log4J (http://jakarta.apache.org/log4j) or JDK 1.4 Logging as the logging implementation. Many developers prefer Log4J even if they are using JDK 1.3. Log4J is more mature and feature-rich than JDK 1.4 Logging and is easily configured. Log4J reads its settings from a configuration file, log4j.properties. Classes write logging messages using a logger. In most cases, the logger name matches the fully qualified class name. For each logger, you can set the logging level and the destination of the log messages, known as an appender. With Log4J, you can set the log level and the appenders with one property. For example, you could configure the Struts RequestProcessor to log debug messages to the console with this property: log4j.logger.org.apache.struts.action.RequestProcessor=DEBUG,Console Loggers are hierarchical; therefore, you can set the logging level for all Struts classes like this: log4j.logger.org.apache.struts=DEBUG,Console,File Log4J supports the concept of a default logger, known as the root logger. The root logger is configured like this: log4j.rootLogger=WARN,Console If a log4j.logger property isn't set for a specific logger, the log messages will be written based on the root logger settings. In Example 13-1, the default logging level is set to WARN. Period messages that are warnings, or have a higher severity such as ERROR or FATAL, will be logged, and the messages will be written to the Console appender. Appenders are configured using a log4j.appender property. The console and file appenders shown in Example 13-1 will work on any system. For more details on configuring appenders, see the Log4J documentation at http://logging.apache.org/log4j. Figure 13-1 shows a sample of logging output when the Solution is applied to the struts-example. Figure 13-1. Debug level logging messagesThe information displayed in each log message is based on the layout properties specified for the appender. In the Solution, a series of format specifiers are used: log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p %c - %m%n These format specifiers result in the display of the date (%d), thread (%t), priority (%-5p), class name (%c), log message (%m), and a carriage return (%n). For a complete list of all supported format specifiers, see the documentation for the Log4J PatternLayout class. See AlsoFor additional details o n configuring Struts logging, check the Struts User's Guide at http://struts.apache.org/userGuide/configuration.html#config_logging. You can find information on Commons Logging at http://jakarta.apache.org/commons/logging. For more information on Log4J, point your browser to http://logging.apache.org/log4j. |
|