[logback-user] ContextSelector and getLogger

Robert Elliot rob at lidalia.org.uk
Thu Mar 5 15:33:30 CET 2009

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


I currently try to get SLF4J and Logback to work on IBM Lotus Domino Server
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

	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 =

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?

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

More information about the Logback-user mailing list