[slf4j-user] best practice for naming loggers?

Joern Huxhorn jhuxhorn at googlemail.com
Tue Mar 22 15:42:58 CET 2011


On 21.03.2011, at 00:34, Rusty Wright wrote:

> In the log4j documentation they recommend "statically instantiating a logger in each class, with the logger name equal to the fully qualified name of the class."  For example,
> 
>  static Logger logger = Logger.getLogger(MyApp.class);
> 
> I've never understood why it's necessary to declare the logger static.  I've assumed/guessed that it is to reduce the number of calls to getLogger.
> 
> The slf4j documentation doesn't use static; for example,
> 
>  final Logger logger = LoggerFactory.getLogger(Wombat.class);
> 
> I've also wondered why everyone uses .class; I've been using this.getClass(); for example,
> 
>  private final transient Logger log = LoggerFactory.getLogger(this.getClass());
> 

There is one more thing to be aware of:
If you use this.getClass() in Wombat and you extend it from the class MyWombat then all loggers contained in the base-class will also log using the logger name MyWombat.
Some would say that this is actually a nice feature but I'd argue that it's confusing to see a log message with a different logger name than the class the logger call is actually contained in.

I'm not saying that it's wrong to create a logger like this... just be careful and know what you are doing ;)

Cheers,
Joern.


More information about the slf4j-user mailing list