[logback-user] ContextSelector and getLogger

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


Hello Ceki,

Java on Domino means, you are working with 3 types of processes
Agents: Every Agent has it's on VM, It has it's own Session, it could deal 
with static loggers.
WebServices: Every Request has it's own Thread with it's own Session, but 
the VM remains open until the Server shuts down
Servlets: Every Request has it's own Thread with it's own Session, but the 
VM remains open until the Server shuts down

As I said we want to log into a database. To log into a database we need a 
session. A session is available for a Thread. This means for WebServices 
and Servlets, that we need a way to find the correct DatabaseAppender for 
the corresponding thread with the corresponding session from static 
Loggers. That's why I thought about the ContextSelector.

_______________________________
Martin Burchard
 
 
Tel: +49 89 54493730
Fax: +49 89 54493737
Mob: +49-(0)172-8520299
Skype: nabor_gilgalad
martin.burchard at pentos.com
_______________________________
_______________________________
Pentos AG
 
 
Landsberger Straße 6
80339 München
Tel: +49 89 54493730
Fax: +49 89 54493737
www.pentos.com
_______________________________




From:
Ceki Gulcu <ceki at qos.ch>
To:
logback users list <logback-user at qos.ch>
Date:
05.03.2009 15:32
Subject:
Re: [logback-user] ContextSelector and getLogger
Sent by:
logback-user-bounces at qos.ch




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
_______________________________________________
Logback-user mailing list
Logback-user at qos.ch
http://qos.ch/mailman/listinfo/logback-user


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://qos.ch/pipermail/logback-user/attachments/20090305/9effa24c/attachment-0001.htm>


More information about the Logback-user mailing list