Added by jgarnett, last edited by jgarnett on Aug 31, 2008  (view change)

Labels

 
(None)

The org.geotools.util package provides a utility class in order to configure how GeoTools handles logging.

Specifically we want to handle logging the way your application is handling logging - but we need a bit of help in order to accomplish that.

For Applications Using Java Logging

GeoTools works out of the box with the java logging system.

For Applications other logging framework

We have provided Logger subclasses that redirects GeoTools log messages to Apache's Commons-Logging framework or Log4J. To setup for the GeoTools library, invoke Logging.setLoggerFactory(String) with the fully qualified class name of a LoggerFactory. The example below tries to setup Commons-Logging first, and fallback on Log4J if the former is not present on the classpath.

final Logging logging = Logging.ALL;
try {
    logging.setLoggerFactory("org.geotools.util.logging.CommonsLoggerFactory");
} catch (ClassNotFoundException commonsException) {
    try {
        logging.setLoggerFactory("org.geotools.util.logging.Log4JLoggerFactory");
    } catch (ClassNotFoundException log4jException) {
        // Nothing to do, we already tried our best.
    }
}

Note that ClassNotFoundException is a checked exception thrown if Commons-Logging or Log4J is not available on the classpath, so GeoTools continue to rely on the Java logging system instead.

Providing explicit LoggerFactory

The above calls could be replaced by the following one with more type safety:

Logging.ALL.setLoggerFactory(Log4JLoggerFactory.getInstance());

However the later is a good approach only if the Log4J framework is certain to be present on the classpath. If it is not, then this method call has unpredictable behavior. It will typically throws a NoClassDefFoundError (the unchecked error, not the checked exception) at some future point. The error may not be thrown at the moment setLoggerFactory is invoked, but rather be delayed until a message is first logged, which may surprise the user.

An other approach is to use org.geotools.factory.GeoTools which has a init method performing some system-wide settings, including invoking the above. Users who don't want common logging just need to not provide its JAR on the classpath.