[slf4j-user] Idiom for declaring loggers

Ceki Gulcu listid at qos.ch
Tue Apr 29 22:41:04 CEST 2008


Hello all,


A common idiom for logging is to create a logger in each class that is
based on the class name.

package some.package;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyClass {
   final Logger logger = LoggerFactory.getLogger(MyClass.class);
   ... etc
}

As Heinz Kabutz describes [1], the name of the class is then
duplicated in the class, both in the class definition and in the
logger field definition.

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
}

If the hosting class is serializable, then the logger variable needs
to be declared as transient.

Does anyone know of a better alternative?

[1] http://www.javaspecialists.co.za/archive/newsletter.do?issue=137

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