[logback-user] ContextSelector and getLogger

Ceki Gulcu ceki at qos.ch
Thu Mar 5 18:11:41 CET 2009


See http://logback.qos.ch/manual/appenders.html#SiftingAppender
which will work for all loggers, even those declared statically.

You could either put values in MDC when your thread starts or develop your own 
"discriminator" class based on thread ids. A discriminator has to implement
the  ch.qos.logback.core.sift.Discriminator interface.

Here is a tentative implementation:

package myPackage;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.sift.Discriminator;
import ch.qos.logback.core.spi.ContextAwareBase;

public class ThreadBasedDiscriminator extends ContextAwareBase implements 
Discriminator<ILoggingEvent>{

   boolean started = false;

   public String getDiscriminatingValue(ILoggingEvent e) {
     // discriminate based on the id of the current thread
     // it could also be the "Domino database" for the current thread
     int id = System.identityHashCode(Thread.currentThread());
     return Integer.toString(id);
   }

   public String getKey() {
     // always use the same fixed key
     return "threadId";
   }

   public boolean isStarted() {
     return started;
   }

   public void start() {
     started = true;
   }

   public void stop() {
     started = false;
   }
}

With the help of this discriminator each thread will use its own specific 
appender. You could modify the discriminator so that several related thread map 
to the same value so that they use the same appender.

I fear that I still have not understood your environment very well. Thus, the 
above proposal might be off the mark.

HTH,

Martin Burchard wrote:
> Hello Mahoney,
> 
> I read about MDC and Filter but I don't have a clue how to find the non
> static session object and the non static database object to write the log
> into the right database object.
> 
> The only other way, without using the ContextSelector is to create an
> Appender that tries to find the corresponding Session with ThreadLocal
> access. But I think that this wont be very fast...
> 
> 


-- 
Ceki Gülcü
Logback: The reliable, generic, fast and flexible logging framework for Java.
http://logback.qos.ch


More information about the Logback-user mailing list