org.apache.jetspeed.webapp.logging
Class IsolatedLog4JLogger

java.lang.Object
  extended by org.apache.jetspeed.webapp.logging.IsolatedLog4JLogger
All Implemented Interfaces:
org.apache.commons.logging.Log

public class IsolatedLog4JLogger
extends Object
implements org.apache.commons.logging.Log

IsolatedLog4JLogger routes all commons-logging logging using Log4J to an ContextClassLoader specific Hierarchy.

For web or application servers providing (and enforcing) commons-logging and Log4J from a shared context (like JBoss), configuring Log4J loggers and appenders from within a (web) application overrides and resets the global Log4J LoggerRepository.

Capturing root logging for logging events from within the web application for example isn't possible using a Log4J propery or xml configuration without routing ALL root logging through the new (web application specific) configuration.

It is possible using the Log4J API directly instead of configuration files, but that requires hardcoded knowledge about how the logging is to be done.

Furthermore, if another application later on reconfigures the root logging again, the current root logging configuration is closed, detached and rerouted to the new configuration.

The same applies of course to common named loggers like capturing springframework logging.

The only way to prevent this stealing of logging configuration is allowing only one log4J configuration for the whole web or application server.
As this has to be done in a web or application server specific manner, setting up Jetspeed for different servers will become rather complex and difficult to automate.

The IsolatedLog4JLogger solves these problems by routing all logging through a statically configured ContextClassLoader isolated LoggerRepository.

Using this class requires a commons-logging.properties file in the WEB-INF/classes folder containing:
   org.apache.commons.logging.Log=org.apache.jetspeed.util.IsolatedLog4JLogger
 

During web application initialization, preferably from a ServletContextListener or a Servlet init method loaded with a load-on-startup value of 0 (zero), a new ContextClassLoader (e.g. web application) specific LoggerRepository as well as the initialization of Log4J should be configured as follows:

   Properties p = new Properties();
   p.load(new FileInputStream(log4jFile));
   // Create a new LoggerRepository
   Hierarchy h = new Hierarchy(new RootCategory(Level.INFO));
   // Configure the LoggerRepository with our own Log4J configuration
   new PropertyConfigurator().doConfigure(p,h);
   // set the LoggerRepository to be used for the current ContextClassLoader
   IsolatedLog4JLogger.setHierarchy(h);
 
Instead of using a PropertyConfigurator a DomConfigurator could be used likewise.

TODO: It might be useful to have this utility class available for usage by Portlet Applications too. Because this class must be included within a web application classpath, a separate jetspeed-tools or jetspeed-utils library will have to be created for it (and possibly other utility classes as well) which then can be put in the web application lib folder.

Version:
$Id: IsolatedLog4JLogger.java 531463 2007-04-23 13:35:58Z taylor $
Author:
Ate Douma

Field Summary
private static org.apache.log4j.Hierarchy hierarchy
           
private  org.apache.commons.logging.impl.Log4JLogger logger
           
private static HashMap notIsolatedLoggers
           
 
Constructor Summary
IsolatedLog4JLogger(String name)
           
 
Method Summary
 void debug(Object arg0)
           
 void debug(Object arg0, Throwable arg1)
           
 boolean equals(Object obj)
           
 void error(Object arg0)
           
 void error(Object arg0, Throwable arg1)
           
 void fatal(Object arg0)
           
 void fatal(Object arg0, Throwable arg1)
           
private  org.apache.commons.logging.impl.Log4JLogger getLogger()
           
 void info(Object arg0)
           
 void info(Object arg0, Throwable arg1)
           
 boolean isDebugEnabled()
           
 boolean isErrorEnabled()
           
 boolean isFatalEnabled()
           
 boolean isInfoEnabled()
           
 boolean isTraceEnabled()
           
 boolean isWarnEnabled()
           
static void setHierarchy(org.apache.log4j.Hierarchy hierarchy)
           
private  void setLogger(org.apache.commons.logging.impl.Log4JLogger logger)
           
 String toString()
           
 void trace(Object arg0)
           
 void trace(Object arg0, Throwable arg1)
           
 void warn(Object arg0)
           
 void warn(Object arg0, Throwable arg1)
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

hierarchy

private static org.apache.log4j.Hierarchy hierarchy

notIsolatedLoggers

private static HashMap notIsolatedLoggers

logger

private org.apache.commons.logging.impl.Log4JLogger logger
Constructor Detail

IsolatedLog4JLogger

public IsolatedLog4JLogger(String name)
Method Detail

setHierarchy

public static void setHierarchy(org.apache.log4j.Hierarchy hierarchy)

setLogger

private void setLogger(org.apache.commons.logging.impl.Log4JLogger logger)

getLogger

private org.apache.commons.logging.impl.Log4JLogger getLogger()

debug

public void debug(Object arg0)
Specified by:
debug in interface org.apache.commons.logging.Log

debug

public void debug(Object arg0,
                  Throwable arg1)
Specified by:
debug in interface org.apache.commons.logging.Log

equals

public boolean equals(Object obj)
Overrides:
equals in class Object

error

public void error(Object arg0)
Specified by:
error in interface org.apache.commons.logging.Log

error

public void error(Object arg0,
                  Throwable arg1)
Specified by:
error in interface org.apache.commons.logging.Log

fatal

public void fatal(Object arg0)
Specified by:
fatal in interface org.apache.commons.logging.Log

fatal

public void fatal(Object arg0,
                  Throwable arg1)
Specified by:
fatal in interface org.apache.commons.logging.Log

info

public void info(Object arg0)
Specified by:
info in interface org.apache.commons.logging.Log

info

public void info(Object arg0,
                 Throwable arg1)
Specified by:
info in interface org.apache.commons.logging.Log

isDebugEnabled

public boolean isDebugEnabled()
Specified by:
isDebugEnabled in interface org.apache.commons.logging.Log

isErrorEnabled

public boolean isErrorEnabled()
Specified by:
isErrorEnabled in interface org.apache.commons.logging.Log

isFatalEnabled

public boolean isFatalEnabled()
Specified by:
isFatalEnabled in interface org.apache.commons.logging.Log

isInfoEnabled

public boolean isInfoEnabled()
Specified by:
isInfoEnabled in interface org.apache.commons.logging.Log

isTraceEnabled

public boolean isTraceEnabled()
Specified by:
isTraceEnabled in interface org.apache.commons.logging.Log

isWarnEnabled

public boolean isWarnEnabled()
Specified by:
isWarnEnabled in interface org.apache.commons.logging.Log

toString

public String toString()
Overrides:
toString in class Object

trace

public void trace(Object arg0)
Specified by:
trace in interface org.apache.commons.logging.Log

trace

public void trace(Object arg0,
                  Throwable arg1)
Specified by:
trace in interface org.apache.commons.logging.Log

warn

public void warn(Object arg0)
Specified by:
warn in interface org.apache.commons.logging.Log

warn

public void warn(Object arg0,
                 Throwable arg1)
Specified by:
warn in interface org.apache.commons.logging.Log


Copyright © 1999-2007 Apache Software Foundation. All Rights Reserved.