logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bender Heri" <HBen...@Ergonomics.ch>
Subject RE: Re: static logger
Date Tue, 08 Jul 2008 08:53:29 GMT

The MDC which is commonly used to distinguish different Repositories in different contexts
is already based on ThreadLocal. If you make the logger instance thread locale you do not
gain anything versus declaring the logger non static. In contrary: Each new thread would instantiate
a new logger instance even if the new thread runs within a context which has already instantiated
the desired logger.

Using non static loggers is a must in library classes which can run in different contexts
which require different repositories (means here different file appender targets). Static
methods of utility classes must even fetch the correct logger on each call as local variable.

Using ThreadLocale directly would only be applicable if really each new Thread has to log
to different files. But in the cases where you have defined number of sub applications whose
log output should be separated (an which can run again and again in new Threads) the ThreadLocal
would be overkill.

Fetching an already instantiated logger is in fact only an assignment of the logger reference
to the member, regardeless if the member is static or not.

The library recommondation of declaring logger as static members is only valid if
a) The whole app works only with one Logger repository (the default)
b) the class which instantiates the logger runs only within a context which is associated
to one distinct repository.


> -----Original Message-----
> From: Java House [mailto:java4dev@gmail.com] 
> Sent: Saturday, July 05, 2008 6:38 PM
> To: Log4J Developers List
> Subject: [SPAM (Bayesain Analysis)] - Re: static logger - 
> Bayesian Filter detected spam
> Great suggestion!!!
> I think this will keep me happy and satisfy library's recomendations.
> Thanks
> O/H Thorbjørn Ravn Andersen έγραψε:
> > Java House skrev  den 05-07-2008 18:10:
> >> O/H Thorbjørn Ravn Andersen έγραψε:
> >>> Java House skrev  den 05-07-2008 16:27:
> >>>> How bad is it, to NOT use static logger;
> >>>
> >>> By having the logger variable static it means it is only 
> initialized 
> >>> once when the class is loaded where as a non-static logger is 
> >>> initialized each time you create a new object from the class.  If 
> >>> you do this a lot there might be a substantial overhead which is 
> >>> often unnecessary if the logger is the same for all objects.
> >> I understand this.
> > I believe that is why it is disrecommended.
> >>> Please note that the object may be used by more than one 
> thread so 
> >>> you should be aware that this might trick your RepositorySelector.
> >>>
> >> But this is exactly the problem I have by using a static logger 
> >> object. The logger variable gets value from the last instantiated 
> >> object which is any of the threads in question. The only 
> way to make 
> >> this work is if the logger object is not static.
> > Are you familiar with the ThreadLocal class?  Sounds to me 
> that that 
> > is what you might want to look into.
> >
> > http://java.sun.com/j2se/1.4.2/docs/api/java/lang/ThreadLocal.html
> >
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-dev-help@logging.apache.org
View raw message