7.11. Specifying a Logging Implementation

Problem

You are using a component that writes log messages with Commons Logging, and you need to configure the underlying logging implementation.

Solution

If the system property org.apache.commons.logging.Log is not set, Commons Logging will use Apache Log4J if it is available in the classpath. To explicitly configure Commons Logging to use Log4J, set the org.apache.commons.logging.Log property to org.apache.commons.logging.impl.Log4JLogger with the following statement:

System.setProperty( "org.apache.commons.logging.Log",
                    "org.apache.commons.logging.impl.Log4JLogger" );

If the system property org.apache.commons.logging.Log is not set and Apache Log4J is not available on the classpath, Commons Logging will then use the built-in JDK 1.4 logging framework. To explicitly configure Commons Logging to use the JDK 1.4 logging framework, set the org.apache.commons.logging.Log property to org.apache.commons.logging.impl.Jdk14Logger.

If neither Apache Log4J nor the JDK 1.4 logging framework is available on the classpath and the org.apache.commons.logging.Log system property is not set, Commons Logging uses a basic logging implementation named SimpleLog. To explicitly configure Commons Logging to use SimpleLog, set the org.apache.commons.logging.Log property to org.apache.commons.logging.impl.SimpleLog.

Discussion

To summarize, Commons Logging performs the following steps when choosing an underlying logging implementation:

  1. Checks the org.apache.commons.logging.Log system property. If this property is set, use the class specified in this property.

  2. Checks for the presence of Log4J in the classpath. If Log4J is present, use a Log4JLogger instance.

  3. Checks for the presence of the JDK 1.4 logging framework. If JDK 1.4 is present, use a JDK14Logger instance.

If neither Log4J nor JDK 1.4 is available, use SimpleLog.

See Also

For more information about the configuration of Apache Log4J, see Recipe 7.13 and Recipe 7.14. For more information about the configuration of the JDK 1.4 logging framework, see Sun’s documentation of this framework at http://java.sun.com/j2se/1.4.2/docs/guide/util/logging/.

SimpleLog is a very simple logger that can be used when an application does not need a complex logging framework. For more information about configuring SimpleLog via system properties, read the Commons Logging JavaDoc at http://jakarta.apache.org/commons/logging/api/index.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.79.147