3.6. Custom Log

If none of available logging systems meet your needs, the logging system can be configured to use a custom logging class. You might use custom logging to integrate with a proprietary logging framework used by some applications servers, or for logging to a graphical component for GUI applications.

A custom logging framework must include an implementation of the org.apache.commons.logging.LogFactory class. To plug your implementation into the runtime, set the org.apache.commons.logging.LogFactory system property to the full name of your log factory class. We present a custom log class example below.

Example 3.6. Custom Logging Class

import org.apache.commons.logging.*;
import org.apache.commons.logging.impl.*;


public class CustomLoggingExample
    extends LogFactoryImpl
{   
    public Log getInstance (String name)
    {
        // return a simple extension of SimpleLog that will log
        // everything to the System.err stream
        return new SimpleLog (name)
        {
            /**
             *  In our example, all log levels are enabled.
             */
            protected boolean isLevelEnabled (int logLevel)
            {
                return true;
            }
        
            /**
             *  Just send everything to System.err
             */
            protected void log (int type, Object message, Throwable t)
            {
                System.err.println ("CUSTOM_LOG: " + type + ": "
                    + message + ": " + t);
            }
        };
    }
}