RSS Feed for This PostCurrent Article

Java: Logger Configuration

Normally if you want to avoid creating multiple logger in your based and derived classes, you can declare a single logger in the based class.

E.g. in the following base class, a single logger is declared

   1: package twit88;
   2:  
   3: import org.apache.commons.logging.Log;
   4: import org.apache.commons.logging.LogFactory;
   5:  
   6: public class BaseLogger {
   7:  
   8:     protected static final Log log = LogFactory.getLog(BaseLogger.class);
   9:     
  10: }

The in your derived class, you can use the logger directly, and the class is shown in the log correctly

   1: package twit88;
   2:  
   3: import org.apache.commons.logging.Log;
   4: import org.apache.commons.logging.LogFactory;
   5:  
   6: public class DerivedClass1 extends BaseLogger {
   7:     public static void log(String msg) {
   8:         log.info(msg);
   9:     }
  10: }

The output

   1: 2008-10-02 00:27:30,093 |  INFO | [DerivedClass1:11 log | main] - logging class 1

 

Even though the logger is declared in the base class, it manage to know the class that invoke it correctly.

The only problem is that you only can control the log level at the base class, not at the derived class any more.

   1: <logger name="twit88.BaseLogger" additivity="true">
   2:     <level value="INFO"/>
   3: </logger>
   4: <logger name="twit88.DerivedClass1" additivity="true">
   5:     <level value="ERROR"/>
   6: </logger>

The logger configuration including additivity for “twit88.DerivedClass1” has no effect now since the logger is declared at the base class.


Trackback URL


Sorry, comments for this entry are closed at this time.