Recipe13.2.Configuring Struts Logging


Recipe 13.2. Configuring Struts Logging

Problem

You need to set the severity level and details of the log messages generated by internal Struts components.

Solution

Configure Struts logging to use Log4J as its logging implementation:

  1. Download the Log4J jar file from http://jakarta.apache.org/log4j.

  2. Copy the jar file to your application's WEB-INF/lib folder.

  3. Set the log level and log destination (referred to as an appender) for all Struts packages (org.apache.struts), as shown in Example 13-1 (log4j.properties). Place this file in your application's WEB-INF/classes folder.

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

  1. Configure Commons Logging to use Log4J in the commons-logging.properties file shown in Example 13-2. Save this file in your WEB-INF/classes directory.

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

Discussion

If 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 messages


The 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 Also

For 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.



    Jakarta Struts Cookbook
    Jakarta Struts Cookbook
    ISBN: 059600771X
    EAN: 2147483647
    Year: 2005
    Pages: 200

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net