[slf4j-dev] svn commit: r1316 - in slf4j/trunk/jcl-over-slf4j: . src/main/java/org/apache/commons/logging/impl src/test/java/org/apache/commons/logging/impl
ceki at slf4j.org
ceki at slf4j.org
Fri Apr 17 20:03:00 CEST 2009
Author: ceki
Date: Fri Apr 17 20:02:59 2009
New Revision: 1316
Modified:
slf4j/trunk/jcl-over-slf4j/pom.xml
slf4j/trunk/jcl-over-slf4j/src/main/java/org/apache/commons/logging/impl/SLF4JLocationAwareLog.java
slf4j/trunk/jcl-over-slf4j/src/main/java/org/apache/commons/logging/impl/SLF4JLog.java
slf4j/trunk/jcl-over-slf4j/src/test/java/org/apache/commons/logging/impl/SerializationTest.java
Log:
- Implementations of Log interface in the jcl-over-slf4j module now support serialization.
This fixes bug 79.
Modified: slf4j/trunk/jcl-over-slf4j/pom.xml
==============================================================================
--- slf4j/trunk/jcl-over-slf4j/pom.xml (original)
+++ slf4j/trunk/jcl-over-slf4j/pom.xml Fri Apr 17 20:02:59 2009
@@ -19,12 +19,19 @@
</description>
<dependencies>
+ <!--
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
- </dependency>
+ </dependency>
+ -->
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-jdk14</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
Modified: slf4j/trunk/jcl-over-slf4j/src/main/java/org/apache/commons/logging/impl/SLF4JLocationAwareLog.java
==============================================================================
--- slf4j/trunk/jcl-over-slf4j/src/main/java/org/apache/commons/logging/impl/SLF4JLocationAwareLog.java (original)
+++ slf4j/trunk/jcl-over-slf4j/src/main/java/org/apache/commons/logging/impl/SLF4JLocationAwareLog.java Fri Apr 17 20:02:59 2009
@@ -16,32 +16,40 @@
package org.apache.commons.logging.impl;
+import java.io.ObjectStreamException;
import java.io.Serializable;
import org.apache.commons.logging.Log;
import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.slf4j.spi.LocationAwareLogger;
/**
- * Implementation of {@link Log org.apache.commons.logging.Log} interface which
- * delegates all processing to a wrapped {@link Logger org.slf4j.Logger} instance.
+ * Implementation of {@link Log org.apache.commons.logging.Log} interface which
+ * delegates all processing to a wrapped {@link Logger org.slf4j.Logger}
+ * instance.
*
- * <p>JCL's FATAL level is mapped to ERROR. All other levels map one to one.
+ * <p>
+ * JCL's FATAL level is mapped to ERROR. All other levels map one to one.
*
* @author Ceki Gülcü
*/
public class SLF4JLocationAwareLog implements Log, Serializable {
private static final long serialVersionUID = -2379157579039314822L;
-
- // in both Log4jLogger and Jdk14Logger classes in the original JCL, the
+
+ //used to store this logger's name to recreate it after serialization
+ protected String name;
+
+ // in both Log4jLogger and Jdk14Logger classes in the original JCL, the
// logger instance is transient
private transient LocationAwareLogger logger;
private static final String FQCN = SLF4JLocationAwareLog.class.getName();
-
+
SLF4JLocationAwareLog(LocationAwareLogger logger) {
this.logger = logger;
+ this.name = logger.getName();
}
/**
@@ -51,14 +59,14 @@
public boolean isTraceEnabled() {
return logger.isTraceEnabled();
}
-
+
/**
* Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
*/
public boolean isDebugEnabled() {
return logger.isDebugEnabled();
}
-
+
/**
* Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
*/
@@ -72,7 +80,7 @@
public boolean isWarnEnabled() {
return logger.isWarnEnabled();
}
-
+
/**
* Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
*/
@@ -88,133 +96,171 @@
return logger.isErrorEnabled();
}
-
/**
- * Converts the input parameter to String and then delegates to
- * the debug method of the wrapped <code>org.slf4j.Logger</code> instance.
+ * Converts the input parameter to String and then delegates to the debug
+ * method of the wrapped <code>org.slf4j.Logger</code> instance.
*
- * @param message the message to log. Converted to {@link String}
+ * @param message
+ * the message to log. Converted to {@link String}
*/
public void trace(Object message) {
- logger.log(null, FQCN, LocationAwareLogger.TRACE_INT, String.valueOf(message), null);
+ logger.log(null, FQCN, LocationAwareLogger.TRACE_INT, String
+ .valueOf(message), null);
}
/**
- * Converts the first input parameter to String and then delegates to
- * the debug method of the wrapped <code>org.slf4j.Logger</code> instance.
+ * Converts the first input parameter to String and then delegates to the
+ * debug method of the wrapped <code>org.slf4j.Logger</code> instance.
*
- * @param message the message to log. Converted to {@link String}
- * @param t the exception to log
+ * @param message
+ * the message to log. Converted to {@link String}
+ * @param t
+ * the exception to log
*/
public void trace(Object message, Throwable t) {
- logger.log(null, FQCN, LocationAwareLogger.TRACE_INT, String.valueOf(message), t);
+ logger.log(null, FQCN, LocationAwareLogger.TRACE_INT, String
+ .valueOf(message), t);
}
/**
- * Converts the input parameter to String and then delegates to the wrapped
+ * Converts the input parameter to String and then delegates to the wrapped
* <code>org.slf4j.Logger</code> instance.
*
- * @param message the message to log. Converted to {@link String}
+ * @param message
+ * the message to log. Converted to {@link String}
*/
public void debug(Object message) {
- logger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, String.valueOf(message), null);
+ logger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, String
+ .valueOf(message), null);
}
/**
- * Converts the first input parameter to String and then delegates to
- * the wrapped <code>org.slf4j.Logger</code> instance.
+ * Converts the first input parameter to String and then delegates to the
+ * wrapped <code>org.slf4j.Logger</code> instance.
*
- * @param message the message to log. Converted to {@link String}
- * @param t the exception to log
+ * @param message
+ * the message to log. Converted to {@link String}
+ * @param t
+ * the exception to log
*/
public void debug(Object message, Throwable t) {
- logger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, String.valueOf(message), t);
+ logger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, String
+ .valueOf(message), t);
}
/**
- * Converts the input parameter to String and then delegates to the wrapped
+ * Converts the input parameter to String and then delegates to the wrapped
* <code>org.slf4j.Logger</code> instance.
*
- * @param message the message to log. Converted to {@link String}
+ * @param message
+ * the message to log. Converted to {@link String}
*/
public void info(Object message) {
- logger.log(null, FQCN, LocationAwareLogger.INFO_INT, String.valueOf(message), null);
+ logger.log(null, FQCN, LocationAwareLogger.INFO_INT, String
+ .valueOf(message), null);
}
/**
- * Converts the first input parameter to String and then delegates to
- * the wrapped <code>org.slf4j.Logger</code> instance.
+ * Converts the first input parameter to String and then delegates to the
+ * wrapped <code>org.slf4j.Logger</code> instance.
*
- * @param message the message to log. Converted to {@link String}
- * @param t the exception to log
+ * @param message
+ * the message to log. Converted to {@link String}
+ * @param t
+ * the exception to log
*/
public void info(Object message, Throwable t) {
- logger.log(null, FQCN, LocationAwareLogger.INFO_INT, String.valueOf(message), t);
+ logger.log(null, FQCN, LocationAwareLogger.INFO_INT, String
+ .valueOf(message), t);
}
/**
- * Converts the input parameter to String and then delegates to the wrapped
+ * Converts the input parameter to String and then delegates to the wrapped
* <code>org.slf4j.Logger</code> instance.
*
- * @param message the message to log. Converted to {@link String}
+ * @param message
+ * the message to log. Converted to {@link String}
*/
public void warn(Object message) {
- logger.log(null, FQCN, LocationAwareLogger.WARN_INT, String.valueOf(message), null);
+ logger.log(null, FQCN, LocationAwareLogger.WARN_INT, String
+ .valueOf(message), null);
}
/**
- * Converts the first input parameter to String and then delegates to
- * the wrapped <code>org.slf4j.Logger</code> instance.
+ * Converts the first input parameter to String and then delegates to the
+ * wrapped <code>org.slf4j.Logger</code> instance.
*
- * @param message the message to log. Converted to {@link String}
- * @param t the exception to log
+ * @param message
+ * the message to log. Converted to {@link String}
+ * @param t
+ * the exception to log
*/
public void warn(Object message, Throwable t) {
- logger.log(null, FQCN, LocationAwareLogger.WARN_INT, String.valueOf(message), t);
+ logger.log(null, FQCN, LocationAwareLogger.WARN_INT, String
+ .valueOf(message), t);
}
/**
- * Converts the input parameter to String and then delegates to the wrapped
+ * Converts the input parameter to String and then delegates to the wrapped
* <code>org.slf4j.Logger</code> instance.
*
- * @param message the message to log. Converted to {@link String}
+ * @param message
+ * the message to log. Converted to {@link String}
*/
public void error(Object message) {
- logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), null);
+ logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String
+ .valueOf(message), null);
}
/**
- * Converts the first input parameter to String and then delegates to
- * the wrapped <code>org.slf4j.Logger</code> instance.
+ * Converts the first input parameter to String and then delegates to the
+ * wrapped <code>org.slf4j.Logger</code> instance.
*
- * @param message the message to log. Converted to {@link String}
- * @param t the exception to log
+ * @param message
+ * the message to log. Converted to {@link String}
+ * @param t
+ * the exception to log
*/
public void error(Object message, Throwable t) {
- logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), t);
+ logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String
+ .valueOf(message), t);
}
-
-
/**
- * Converts the input parameter to String and then delegates to
- * the error method of the wrapped <code>org.slf4j.Logger</code> instance.
+ * Converts the input parameter to String and then delegates to the error
+ * method of the wrapped <code>org.slf4j.Logger</code> instance.
*
- * @param message the message to log. Converted to {@link String}
+ * @param message
+ * the message to log. Converted to {@link String}
*/
public void fatal(Object message) {
- logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), null);
+ logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String
+ .valueOf(message), null);
}
/**
- * Converts the first input parameter to String and then delegates to
- * the error method of the wrapped <code>org.slf4j.Logger</code> instance.
+ * Converts the first input parameter to String and then delegates to the
+ * error method of the wrapped <code>org.slf4j.Logger</code> instance.
*
- * @param message the message to log. Converted to {@link String}
- * @param t the exception to log
+ * @param message
+ * the message to log. Converted to {@link String}
+ * @param t
+ * the exception to log
*/
public void fatal(Object message, Throwable t) {
- logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), t);
+ logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String
+ .valueOf(message), t);
}
+ /**
+ * Replace this instance with a homonymous (same name) logger returned by
+ * LoggerFactory. Note that this method is only called during deserialization.
+ *
+ * @return logger with same name as returned by LoggerFactory
+ * @throws ObjectStreamException
+ */
+ protected Object readResolve() throws ObjectStreamException {
+ Logger logger = LoggerFactory.getLogger(this.name);
+ return new SLF4JLocationAwareLog((LocationAwareLogger) logger);
+ }
}
\ No newline at end of file
Modified: slf4j/trunk/jcl-over-slf4j/src/main/java/org/apache/commons/logging/impl/SLF4JLog.java
==============================================================================
--- slf4j/trunk/jcl-over-slf4j/src/main/java/org/apache/commons/logging/impl/SLF4JLog.java (original)
+++ slf4j/trunk/jcl-over-slf4j/src/main/java/org/apache/commons/logging/impl/SLF4JLog.java Fri Apr 17 20:02:59 2009
@@ -16,10 +16,12 @@
package org.apache.commons.logging.impl;
+import java.io.ObjectStreamException;
import java.io.Serializable;
import org.apache.commons.logging.Log;
import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* Implementation of {@link Log org.apache.commons.logging.Log} interface which
@@ -34,12 +36,16 @@
private static final long serialVersionUID = 680728617011167209L;
+ //used to store this logger's name to recreate it after serialization
+ protected String name;
+
// in both Log4jLogger and Jdk14Logger classes in the original JCL, the
// logger instance is transient
private transient Logger logger;
SLF4JLog(Logger logger) {
this.logger = logger;
+ this.name = logger.getName();
}
/**
@@ -214,4 +220,15 @@
logger.error(String.valueOf(message), t);
}
+ /**
+ * Replace this instance with a homonymous (same name) logger returned by
+ * LoggerFactory. Note that this method is only called during deserialization.
+ *
+ * @return logger with same name as returned by LoggerFactory
+ * @throws ObjectStreamException
+ */
+ protected Object readResolve() throws ObjectStreamException {
+ Logger logger = LoggerFactory.getLogger(this.name);
+ return new SLF4JLog(logger);
+ }
}
\ No newline at end of file
Modified: slf4j/trunk/jcl-over-slf4j/src/test/java/org/apache/commons/logging/impl/SerializationTest.java
==============================================================================
--- slf4j/trunk/jcl-over-slf4j/src/test/java/org/apache/commons/logging/impl/SerializationTest.java (original)
+++ slf4j/trunk/jcl-over-slf4j/src/test/java/org/apache/commons/logging/impl/SerializationTest.java Fri Apr 17 20:02:59 2009
@@ -1,24 +1,29 @@
package org.apache.commons.logging.impl;
+import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import junit.framework.TestCase;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.slf4j.impl.JDK14LoggerFactory;
+import org.slf4j.spi.LocationAwareLogger;
public class SerializationTest extends TestCase {
+ ObjectInputStream ois;
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos;
-
+
public SerializationTest(String name) {
super(name);
}
protected void setUp() throws Exception {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
super.setUp();
}
@@ -28,14 +33,33 @@
oos.close();
}
-
- public void testSmokeSimple() throws IOException {
+ public void verify() throws IOException, ClassNotFoundException {
+ ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
+ ois = new ObjectInputStream(bis);
+
+ Log readLog = (Log) ois.readObject();
+ // tests that the "private transient Logger logger" field is non-null
+ readLog.debug("");
+ }
+
+ public void testSLF4JLog() throws Exception {
+ JDK14LoggerFactory factory = new JDK14LoggerFactory();
+ SLF4JLog log = new SLF4JLog(factory.getLogger("x"));
+ oos.writeObject(log);
+ verify();
+ }
+
+ public void testSmoke() throws Exception {
Log log = LogFactory.getLog("testing");
oos.writeObject(log);
+ verify();
}
-
- public void testSmokeLocationAware() throws IOException {
- SLF4JLocationAwareLog log = new SLF4JLocationAwareLog(null);
+
+ public void testLocationAware() throws Exception {
+ JDK14LoggerFactory factory = new JDK14LoggerFactory();
+ SLF4JLocationAwareLog log = new SLF4JLocationAwareLog(
+ (LocationAwareLogger) factory.getLogger("x"));
oos.writeObject(log);
+ verify();
}
}
More information about the slf4j-dev
mailing list