[slf4j-user] Idiom for declaring loggers

Ceki Gulcu listid at qos.ch
Sat May 3 19:11:11 CEST 2008



Endre Stølsvik wrote:
>>>> 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.

Hello Endre,

There will be indeed a computational cost for fetching the same logger
over and over again for each instance of the class with a non-static
logger member. The cost compared to a static logger member, jumps from
10 nanoseconds to about 100 nanoseconds. (These are logback numbers,
numbers for log4j are substantially worse.)

Thus, there is no denying that non-static logger members will have a
measurable negative impact on instance creation. This should be
documented properly, at the very least.


> Endre.

-- 
Ceki Gülcü
QOS.ch is looking to hire talented developers in Switzerland.  If
interested, please contact c e k i AT q o s . c h




More information about the slf4j-user mailing list