[logback-user] ContextSelector and getLogger

Martin Burchard martin.burchard at pentos.com
Thu Mar 5 16:14:57 CET 2009


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...


Mahoney wrote:
> 
> Have you considered not bothering with the thread local, logging in the
> normal static way and filtering the output on the basis of the thread name
> using a logback filter?  Or even on the basis of the session's ID, made
> visible to logback via an MDC value?
> 
> Then all 3rd party library logging statements would be handled as well.
> 
> ----- Original Message -----
> From: "Martin Burchard" <martin.burchard at pentos.com>
> To: logback-user at qos.ch
> Sent: Thursday, 5 March, 2009 14:26:41 GMT +00:00 GMT Britain, Ireland,
> Portugal
> Subject: [logback-user]  ContextSelector and getLogger
> 
> 
> Hello,
> 
> I currently try to get SLF4J and Logback to work on IBM Lotus Domino
> Server
> 8.5.
> It's working.
> We need to do logging an a per Thread basis because we log into a Domino
> database. The Java object of a Domino database is based on a session
> object,
> and that can not exist over more then one thread.
> Okay, I created a simple ContextSelector:
> 
> package de.pentos.domino.logging;
> 
> import java.util.Arrays;
> import java.util.List;
> 
> import ch.qos.logback.classic.LoggerContext;
> 
> /**
>  * @author Martin Burchard
>  */
> public class ContextSelector implements
>     ch.qos.logback.classic.selector.ContextSelector {
> 
> 	private final LoggerContext defaultContext;
> 	private static final ThreadLocal<LoggerContext> threadLocal = new
> ThreadLocal<LoggerContext>();
> 
> 	public ContextSelector(final LoggerContext context) {
> 		defaultContext = context;
> 	}
> 
> 	public LoggerContext detachLoggerContext(final String loggerContextName)
> {
> 		return getLoggerContext();
> 	}
> 
> 	public List<String> getContextNames() {
> 		return Arrays.asList(getLoggerContext().getName());
> 	}
> 
> 	public LoggerContext getDefaultLoggerContext() {
> 		return defaultContext;
> 	}
> 
> 	public LoggerContext getLoggerContext() {
> 		LoggerContext lc = threadLocal.get();
> 		if (lc == null) {
> 			threadLocal.set(new LoggerContext());
> 		}
> 		return threadLocal.get();
> 	}
> 
> 	public LoggerContext getLoggerContext(final String name) {
> 		if (getLoggerContext().getName().equals(name)) {
> 			return getLoggerContext();
> 		}
> 		return null;
> 	}
> 
> }
> 
> As long as all Loggers are used like this:
> 
> private Logger log = LoggerFactory.getLogger(DemoWebService.class);
> 
> everything works fine, I get my logging.
> 
> But also this is a common way to get my Logger:
> 
> private static final Logger log =
> LoggerFactory.getLogger(DemoWebService.class);
> 
> Because for WebServices and Servlets the VM is not terminated and these
> Logger Instance will live a long time without ever get refreshed.
> After the first run I don't see logging any more.
> 
> How can I deal with external JARs like Apache HTTP Client and other that
> instantiate the loggers in a static way?
> 
> Regards,
> Martin
> -- 
> View this message in context:
> http://www.nabble.com/ContextSelector-and-getLogger-tp22352655p22352655.html
> Sent from the Logback User mailing list archive at Nabble.com.
> 
> _______________________________________________
> Logback-user mailing list
> Logback-user at qos.ch
> http://qos.ch/mailman/listinfo/logback-user
> _______________________________________________
> Logback-user mailing list
> Logback-user at qos.ch
> http://qos.ch/mailman/listinfo/logback-user
> 
> 

-- 
View this message in context: http://www.nabble.com/ContextSelector-and-getLogger-tp22352655p22353608.html
Sent from the Logback User mailing list archive at Nabble.com.



More information about the Logback-user mailing list