> -----Original Message-----
> From: Jacob Kjome [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, April 25, 2006 4:36 PM
> To: Log4J Users List
> Subject: RE: Logging of specific session in web app.
>
>
>
> I don't think I've heard of anyone defining a repository
> selector that had two
> distinct criteria for choosing the logger repository. Only
> one selector can be
> installed at any given time. How would Log4j know which
> logger repository to
> choose?
Yes, I did. The criterias are taken from MDC, which is kept per thread basis.
This approach assumes that each sub app which needs its own repository runs
within its own thread(s) and sets the correct MDC values somewhere at the
beginning of run(). Afterwards the finetuning of the loggers can be done (i.e.
setting sophisticated file names of file appenders). Code of my selector see
below.
In the main class of the application you place code like this (before any other
call to log4j framework):
private static Object guard = new Object();
static
{
LogManager.setRepositorySelector( new PMSRepositorySelector(), guard );
}
Within a web app you don't have a main class where you can do this, but it can
be solved by a wrapper class which keeps track wheter this initialization was
already done or not and does it only the very first time.
Heri
Here is the code of my PMSRepositorySelector:
import java.net.URL;
import java.util.Hashtable;
import org.apache.log4j.Hierarchy;
import org.apache.log4j.Level;
import org.apache.log4j.MDC;
import org.apache.log4j.helpers.Loader;
import org.apache.log4j.helpers.OptionConverter;
import org.apache.log4j.spi.LoggerRepository;
import org.apache.log4j.spi.RepositorySelector;
import org.apache.log4j.spi.RootCategory;
/**
* PMSRepositorySelector maintains a separate Hierarchy (LoggerRepository) per
EventID and AppName.
* <p>
* Both values must be set previously within the MDC (log4J class which holds
key/value pairs which can
* be different in different threads). If this MDC values are not set, we
return a default Hierarchy.
* <p>
* The repository is organized within a hashtable. The index of the entry of
the default hierarchy is
* DEFAULT_REPOSITORY_ID, the value of it is the reference to the default
Hierarchy instance. The index of
* all others entries is an Integer class of the respective EventID, the value
is annother Hashtable with a
* String containing the AppName as indices. The values therein are the
different Hierarchy instances.
*
*
* @author bender
*/
public class PMSRepositorySelector implements RepositorySelector
{
/** ID of the default hierarchy */
private static final String DEFAULT_REPOSITORY_ID = "PMSDefaultRepository";
/** the location of the repository */
private static Hashtable myHt = new Hashtable();
/**
* @see org.apache.log4j.spi.RepositorySelector#getLoggerRepository()
*/
public LoggerRepository getLoggerRepository()
{
Integer eventID = ( Integer ) MDC.get( PMSLogger.PROP_NAME_EVENT_ID );
String appName = ( String ) MDC.get( PMSLogger.PROP_NAME_APP_NAME );
Hierarchy h = null;
if ( ( eventID == null )
|| ( appName == null ) )
{
// MDC values are not set. This means, this method was called from
somewhere not in PMS System
h = ( Hierarchy ) myHt.get( DEFAULT_REPOSITORY_ID );
if ( h == null )
{
h = createAndLoadHierarchy();
myHt.put( DEFAULT_REPOSITORY_ID, h );
}
}
else
{
// we maintain one Hierarchy per eventID and appName:
Hashtable ht = ( Hashtable ) myHt.get( eventID );
if ( ht == null )
{
ht = new Hashtable();
myHt.put( eventID, ht );
}
h = ( Hierarchy ) ht.get( appName );
if ( h == null )
{
h = createAndLoadHierarchy();
ht.put( appName, h );
}
}
return h;
}
/**
* Creates a new Hierarchy and configurs it from the file log4j.xml
*
* @return the newly created Hierarchy
*/
private synchronized Hierarchy createAndLoadHierarchy()
{
Hierarchy h;
h = new Hierarchy( new RootCategory( Level.DEBUG ) );
URL url = Loader.getResource( "log4j.xml" );
OptionConverter.selectAndConfigure( url,
"org.apache.log4j.xml.DOMConfigurator", h );
return h;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]