View Javadoc

1   package org.slf4j.helpers;
2   
3   import java.io.ObjectStreamException;
4   import java.io.Serializable;
5   
6   import org.slf4j.Logger;
7   import org.slf4j.LoggerFactory;
8   
9   /**
10   * Serves as base class for named logger implementation. More significantly, this
11   * class establishes deserialization behavior. See @see #readResolve. 
12   * 
13   * @author Ceki Gulcu
14   * @since 1.5.3
15   */
16  abstract class NamedLoggerBase implements Logger, Serializable {
17  
18    private static final long serialVersionUID = 7535258609338176893L;
19  
20    protected String name;
21    
22    public String getName() {
23      return name;
24    }
25    
26    /**
27     * Replace this instance with a homonymous (same name) logger returned 
28     * by LoggerFactory. Note that this method is only called during 
29     * deserialization.
30     * 
31     * <p>
32     * This approach will work well if the desired ILoggerFactory is the one
33     * references by LoggerFactory. However, if the user manages its logger hierarchy
34     * through a different (non-static) mechanism, e.g. dependency injection, then
35     * this approach would be mostly counterproductive.
36     * 
37     * @return logger with same name as returned by LoggerFactory
38     * @throws ObjectStreamException
39     */
40    protected Object readResolve() throws ObjectStreamException {
41      // using getName() instead of this.name works even for
42      // NOPLogger
43      return LoggerFactory.getLogger(getName());
44    }
45  
46  }