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

Get Jakarta Commons Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.