[slf4j-user] Idiom for declaring loggers

Endre Stølsvik Endre at stolsvik.com
Wed Apr 30 11:27:48 CEST 2008


>>> 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.



More information about the slf4j-user mailing list