[slf4j-user] best practice for naming loggers?

Maarten Bosteels mbosteels.dns at gmail.com
Mon Mar 21 09:10:09 CET 2011


http://www.slf4j.org/faq.html#declared_static

On Mon, Mar 21, 2011 at 4:36 AM, Thorbjoern Ravn Andersen
<ravn at runjva.com> wrote:
> Den 21/03/11 00.34, Rusty Wright skrev:
>>
>> 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.
>
> You need the logger field to be static if you have any static methods you
> want to do logging in, and not create an explicit logger in the code in each
> static method.
>
> I don't think that it matters in modern logging systems how many times
> getLogger is called.
>>
>> I've also wondered why everyone uses .class; I've been using
>> this.getClass(); for example,
>>
> Because "this.getClass()" only works for instance fields, not static fields.
>
>> The advantage of that is that you never have to worry about changing that
>> line; you can copy and paste it everywhere, and set up eclipse to
>> automatically put that line in when you create a new class.
>>
> You can also make Eclipse put in the name of the current class, so that
> doesn't matter much.
>
>
>> Using this.getClass() has worked for me, unless I need to use the logger
>> in a static method, so I'm wondering if there is something I'm overlooking.
>
> Don't think so.
>
> --
>  Thorbjørn Ravn Andersen  "...plus... Tubular Bells!"
>
> _______________________________________________
> slf4j-user mailing list
> slf4j-user at qos.ch
> http://qos.ch/mailman/listinfo/slf4j-user
>


More information about the slf4j-user mailing list