[logback-user] ContextSelector and getLogger

Ceki Gulcu ceki at qos.ch
Thu Mar 5 15:32:59 CET 2009


Hello Martin,

Could you please describe your use case in your terms without assuming the use 
of a ContextSelector?

Martin Burchard wrote:
> 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

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