[slf4j-dev] svn commit: r1082 - in slf4j/trunk: slf4j-api/src/main/java/org/slf4j/helpers slf4j-jcl/src/main/java/org/slf4j/impl slf4j-jdk14/src/main/java/org/slf4j/impl slf4j-log4j12/src/main/java/org/slf4j/impl slf4j-nop/src/main/java/org/slf4j/impl slf4j-simple/src/main/java/org/slf4j/impl

ceki at slf4j.org ceki at slf4j.org
Wed Jul 30 23:11:24 CEST 2008


Author: ceki
Date: Wed Jul 30 23:11:23 2008
New Revision: 1082

Added:
   slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/NamedLoggerBase.java
Modified:
   slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/MarkerIgnoringBase.java
   slf4j/trunk/slf4j-jcl/src/main/java/org/slf4j/impl/JCLLoggerAdapter.java
   slf4j/trunk/slf4j-jdk14/src/main/java/org/slf4j/impl/JDK14LoggerAdapter.java
   slf4j/trunk/slf4j-log4j12/src/main/java/org/slf4j/impl/Log4jLoggerAdapter.java
   slf4j/trunk/slf4j-nop/src/main/java/org/slf4j/impl/NOPLogger.java
   slf4j/trunk/slf4j-simple/src/main/java/org/slf4j/impl/SimpleLogger.java

Log:
- fix bug 71 by adding a readResolve method in all Logger implementations. The 
  readResolve method returns the value computed by LoggerFactory.getLogger(name)
  where name is the serialized value of name for the current instance.

Modified: slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/MarkerIgnoringBase.java
==============================================================================
--- slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/MarkerIgnoringBase.java	(original)
+++ slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/MarkerIgnoringBase.java	Wed Jul 30 23:11:23 2008
@@ -36,7 +36,7 @@
  * 
  * @author Ceki Gulcu
  */
-public abstract class MarkerIgnoringBase implements Logger {
+public abstract class MarkerIgnoringBase extends NamedLoggerBase implements Logger {
 
   public boolean isTraceEnabled(Marker marker) {
     return isTraceEnabled();
@@ -162,4 +162,5 @@
   public String toString() {
 	  return this.getClass().getName()+"("+getName()+")";
   }
+  
 }

Added: slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/NamedLoggerBase.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/NamedLoggerBase.java	Wed Jul 30 23:11:23 2008
@@ -0,0 +1,44 @@
+package org.slf4j.helpers;
+
+import java.io.ObjectStreamException;
+import java.io.Serializable;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Serves as base class for named logger implementation. More significantly, this
+ * class establishes deserialization behavior. See @see #readResolve. 
+ * 
+ * @author Ceki Gulcu
+ * @since 1.5.3
+ */
+abstract class NamedLoggerBase implements Logger, Serializable {
+
+  protected String name;
+  
+  public String getName() {
+    return name;
+  }
+  
+  /**
+   * Replace this instance with a homonymous (same name) logger returned 
+   * by LoggerFactory. Note that this method is only called during 
+   * deserialization.
+   * 
+   * <p>
+   * This approach will work well if the desired ILoggerFactory is the one
+   * references by LoggerFactory. However, if the user manages its logger hierarchy
+   * through a different (non-static) mechanism, e.g. dependency injection, then
+   * this approach would be mostly counterproductive.
+   * 
+   * @return logger with same name as returned by LoggerFactory
+   * @throws ObjectStreamException
+   */
+  protected Object readResolve() throws ObjectStreamException {
+    // using getName() instead of this.name works even for
+    // NOPLogger
+    return LoggerFactory.getLogger(getName());
+  }
+
+}

Modified: slf4j/trunk/slf4j-jcl/src/main/java/org/slf4j/impl/JCLLoggerAdapter.java
==============================================================================
--- slf4j/trunk/slf4j-jcl/src/main/java/org/slf4j/impl/JCLLoggerAdapter.java	(original)
+++ slf4j/trunk/slf4j-jcl/src/main/java/org/slf4j/impl/JCLLoggerAdapter.java	Wed Jul 30 23:11:23 2008
@@ -38,8 +38,9 @@
  * @author Ceki G&uuml;lc&uuml;
  */
 public final class JCLLoggerAdapter extends MarkerIgnoringBase {
+
+  private static final long serialVersionUID = 4141593417490482209L;
   final Log log;
-  final String name;
   
   // WARN: JCLLoggerAdapter constructor should have only package access so
   // that only JCLLoggerFactory be able to create one.
@@ -48,10 +49,6 @@
     this.name = name;
   }
 
-  public String getName() {
-    return name;
-  }
-
   /**
    * Delegates to the {@link Log#isTraceEnabled} method of the underlying
    * {@link Log} instance. 

Modified: slf4j/trunk/slf4j-jdk14/src/main/java/org/slf4j/impl/JDK14LoggerAdapter.java
==============================================================================
--- slf4j/trunk/slf4j-jdk14/src/main/java/org/slf4j/impl/JDK14LoggerAdapter.java	(original)
+++ slf4j/trunk/slf4j-jdk14/src/main/java/org/slf4j/impl/JDK14LoggerAdapter.java	Wed Jul 30 23:11:23 2008
@@ -53,16 +53,16 @@
  */
 public final class JDK14LoggerAdapter extends MarkerIgnoringBase implements
     LocationAwareLogger {
+
+  private static final long serialVersionUID = -8053026990503422791L;
+
   final java.util.logging.Logger logger;
 
   // WARN: JDK14LoggerAdapter constructor should have only package access so
   // that only JDK14LoggerFactory be able to create one.
   JDK14LoggerAdapter(java.util.logging.Logger logger) {
     this.logger = logger;
-  }
-
-  public String getName() {
-    return logger.getName();
+    this.name = logger.getName();
   }
 
   /**

Modified: slf4j/trunk/slf4j-log4j12/src/main/java/org/slf4j/impl/Log4jLoggerAdapter.java
==============================================================================
--- slf4j/trunk/slf4j-log4j12/src/main/java/org/slf4j/impl/Log4jLoggerAdapter.java	(original)
+++ slf4j/trunk/slf4j-log4j12/src/main/java/org/slf4j/impl/Log4jLoggerAdapter.java	Wed Jul 30 23:11:23 2008
@@ -33,8 +33,12 @@
 
 package org.slf4j.impl;
 
+import java.io.ObjectStreamException;
+import java.io.Serializable;
+
 import org.apache.log4j.Level;
 import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.slf4j.Marker;
 import org.slf4j.helpers.MarkerIgnoringBase;
 import org.slf4j.helpers.MessageFormatter;
@@ -57,8 +61,11 @@
  * @author Ceki G&uuml;lc&uuml;
  */
 public final class Log4jLoggerAdapter extends MarkerIgnoringBase implements
-    LocationAwareLogger {
-  final org.apache.log4j.Logger logger;
+    LocationAwareLogger, Serializable {
+
+  private static final long serialVersionUID = 6182834493563598289L;
+
+  final transient org.apache.log4j.Logger logger;
 
   /**
    * Following the pattern discussed in pages 162 through 168 of "The complete
@@ -75,6 +82,7 @@
   // only Log4jLoggerFactory be able to create one.
   Log4jLoggerAdapter(org.apache.log4j.Logger logger) {
     this.logger = logger;
+    this.name = logger.getName();
     traceCapable = isTraceCapable();
   }
 
@@ -87,10 +95,6 @@
     }
   }
 
-  public String getName() {
-    return logger.getName();
-  }
-
   /**
    * Is this logger instance enabled for the TRACE level?
    * 
@@ -602,4 +606,5 @@
     }
     logger.log(callerFQCN, log4jLevel, msg, t);
   }
+
 }

Modified: slf4j/trunk/slf4j-nop/src/main/java/org/slf4j/impl/NOPLogger.java
==============================================================================
--- slf4j/trunk/slf4j-nop/src/main/java/org/slf4j/impl/NOPLogger.java	(original)
+++ slf4j/trunk/slf4j-nop/src/main/java/org/slf4j/impl/NOPLogger.java	Wed Jul 30 23:11:23 2008
@@ -43,6 +43,9 @@
  * @author Ceki G&uuml;lc&uuml;
  */
 public class NOPLogger extends MarkerIgnoringBase {
+
+  private static final long serialVersionUID = -517220405410904473L;
+
   /**
    * The unique instance of NOPLogger.
    */

Modified: slf4j/trunk/slf4j-simple/src/main/java/org/slf4j/impl/SimpleLogger.java
==============================================================================
--- slf4j/trunk/slf4j-simple/src/main/java/org/slf4j/impl/SimpleLogger.java	(original)
+++ slf4j/trunk/slf4j-simple/src/main/java/org/slf4j/impl/SimpleLogger.java	Wed Jul 30 23:11:23 2008
@@ -62,6 +62,9 @@
  * @author Ceki G&uuml;lc&uuml;
  */
 public class SimpleLogger extends MarkerIgnoringBase {
+  
+  private static final long serialVersionUID = -6560244151660620173L;
+ 
   /**
    * Mark the time when this class gets loaded into memory.
    */
@@ -71,7 +74,6 @@
   private static String INFO_STR = "INFO";
   private static String WARN_STR = "WARN";
   private static String ERROR_STR = "ERROR";
-  String name;
 
   /**
    * Package access allows only {@link SimpleLoggerFactory} to instantiate
@@ -81,10 +83,6 @@
     this.name = name;
   }
 
-  public String getName() {
-    return name;    
-  }
- 
   /**
    * Always returns false.
    * @return always false



More information about the slf4j-dev mailing list