[slf4j-user] Idiom for declaring loggers

Newcomb, Michael-P57487 Michael.Newcomb at gdc4s.com
Wed Apr 30 14:53:41 CEST 2008


private static final Logger log = LoggerFactory.getLogger(<myclass>.class);

IMHO, all loggers should be at a minimum private. It bothers me when you see a log in a file that says:

[ClassC] some log statement

Only to go to ClassC.java and you can't find the log statement! Then you realize that the variable was declared in the super class ClassB.

As far as Endre is concerned he is correct. A static Logger solves this problem as well...

> -----Original Message-----
> From: user-bounces at slf4j.org [mailto:user-bounces at slf4j.org] 
> On Behalf Of Endre Stølsvik
> Sent: Wednesday, April 30, 2008 5:28 AM
> To: User list for the slf4j project
> Subject: Re: [slf4j-user] Idiom for declaring loggers
> 
> >>> The following logger declaration idiom is resistant to 
> >>> cut-and-pasting between classes. However, it assumes that 
> the logger 
> >>> variable is an instance variable of the class.
> >>>
> >>> package some.package;
> >>>
> >>> import org.slf4j.Logger;
> >>> import org.slf4j.LoggerFactory;
> >>> public class MyClass {
> >>>    final Logger logger = LoggerFactory.getLogger(this.getClass());
> >>>    ... etc
> >>> }
> 
> May I just state that this is very dangerous, and should not 
> be exemplified ever, in the event that people that don't 
> understand quite what it embodies may start to use it in the 
> wrong scenario, WHICH IS when the class in question is 
> instantiated often. If this for example is a helper-class 
> instantiated per service call, the getLogger method will 
> become a HUGE synchronized bottleneck.
> 
> Endre.
> _______________________________________________
> user mailing list
> user at slf4j.org
> http://www.slf4j.org/mailman/listinfo/user
> 



More information about the slf4j-user mailing list