You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2014/02/28 17:59:39 UTC

svn commit: r1572998 - in /logging/log4j/log4j2/trunk: log4j-slf4j-impl/pom.xml log4j-slf4j-impl/src/main/java/org/slf4j/impl/SLF4JLogger.java log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/SerializeTest.java src/changes/changes.xml

Author: rgoers
Date: Fri Feb 28 16:59:39 2014
New Revision: 1572998

URL: http://svn.apache.org/r1572998
Log:
LOG4J2-410 - SLf4JLogger is now Serializable.

Added:
    logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/SerializeTest.java
Modified:
    logging/log4j/log4j2/trunk/log4j-slf4j-impl/pom.xml
    logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/main/java/org/slf4j/impl/SLF4JLogger.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml

Modified: logging/log4j/log4j2/trunk/log4j-slf4j-impl/pom.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-slf4j-impl/pom.xml?rev=1572998&r1=1572997&r2=1572998&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-slf4j-impl/pom.xml (original)
+++ logging/log4j/log4j2/trunk/log4j-slf4j-impl/pom.xml Fri Feb 28 16:59:39 2014
@@ -99,6 +99,7 @@
               <excludes>
                 <exclude>**/LoggerTest.java</exclude>
                 <exclude>**/MarkerTest.java</exclude>
+                <exclude>**/SerializeTest.java</exclude>
               </excludes>
               <includes>
                 <include>**/OptionalTest.java</include>

Modified: logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/main/java/org/slf4j/impl/SLF4JLogger.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/main/java/org/slf4j/impl/SLF4JLogger.java?rev=1572998&r1=1572997&r2=1572998&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/main/java/org/slf4j/impl/SLF4JLogger.java (original)
+++ logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/main/java/org/slf4j/impl/SLF4JLogger.java Fri Feb 28 16:59:39 2014
@@ -17,6 +17,7 @@
 package org.slf4j.impl;
 
 import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.message.Message;
 import org.apache.logging.log4j.message.ParameterizedMessage;
 import org.apache.logging.log4j.message.SimpleMessage;
@@ -27,17 +28,23 @@ import org.slf4j.MarkerFactory;
 import org.slf4j.helpers.EventDataConverter;
 import org.slf4j.spi.LocationAwareLogger;
 
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+
 /**
  *
  */
-public class SLF4JLogger implements LocationAwareLogger {
+public class SLF4JLogger implements LocationAwareLogger, Serializable {
 
+    private static final long serialVersionUID = 7869000638091304316L;
     private static final String FQCN = SLF4JLogger.class.getName();
     private static final Marker EVENT_MARKER = MarkerFactory.getMarker("EVENT");
     private final boolean eventLogger;
-    private final AbstractLoggerWrapper logger;
+    private transient AbstractLoggerWrapper logger;
     private final String name;
-    private final EventDataConverter converter;
+    private transient EventDataConverter converter;
 
     public SLF4JLogger(final AbstractLogger logger, final String name) {
         this.logger = new AbstractLoggerWrapper(logger, name, null);
@@ -502,6 +509,27 @@ public class SLF4JLogger implements Loca
         return name;
     }
 
+    /**
+     * Always treat de-serialization as a full-blown constructor, by
+     * validating the final state of the de-serialized object.
+     */
+    private void readObject(ObjectInputStream aInputStream) throws ClassNotFoundException, IOException {
+        //always perform the default de-serialization first
+        aInputStream.defaultReadObject();
+        logger = new AbstractLoggerWrapper((AbstractLogger) LogManager.getLogger(name), name, null);
+        converter = createConverter();
+    }
+
+    /**
+     * This is the default implementation of writeObject.
+     * Customise if necessary.
+     */
+    private void writeObject(ObjectOutputStream aOutputStream
+    ) throws IOException {
+        //perform the default serialization for all non-transient, non-static fields
+        aOutputStream.defaultWriteObject();
+    }
+
     private EventDataConverter createConverter() {
         try {
             Class.forName("org.slf4j.ext.EventData");

Added: logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/SerializeTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/SerializeTest.java?rev=1572998&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/SerializeTest.java (added)
+++ logging/log4j/log4j2/trunk/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/SerializeTest.java Fri Feb 28 16:59:39 2014
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.slf4j;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.apache.logging.log4j.status.StatusLogger;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.slf4j.ext.XLogger;
+import org.slf4j.ext.XLoggerFactory;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ *
+ */
+public class SerializeTest {
+
+
+    private static final String CONFIG = "log4j-test1.xml";
+    private static LoggerContext ctx;
+
+    @BeforeClass
+    public static void setupClass() {
+        System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
+        ctx = (LoggerContext) LogManager.getContext(false);
+        ctx.getConfiguration();
+    }
+
+    @AfterClass
+    public static void cleanupClass() {
+        System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
+        ctx.reconfigure();
+        StatusLogger.getLogger().reset();
+    }
+
+    Logger logger = LoggerFactory.getLogger("LoggerTest");
+    XLogger xlogger = XLoggerFactory.getXLogger("LoggerTest");
+
+    @Test
+    public void testLogger() throws Exception {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream oos = new ObjectOutputStream(baos);
+        oos.writeObject(logger);
+        byte[] data = baos.toByteArray();
+        assertNotNull("No data", data);
+        assertTrue("No data", data.length > 0);
+        ByteArrayInputStream bais = new ByteArrayInputStream(data);
+        ObjectInputStream ois = new ObjectInputStream(bais);
+        Logger copy = (org.slf4j.Logger) ois.readObject();
+        assertNotNull("Unable to restore logger", copy);
+    }
+}

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1572998&r1=1572997&r2=1572998&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Fri Feb 28 16:59:39 2014
@@ -21,6 +21,9 @@
   </properties>
   <body>
     <release version="2.0-rc2?" date="2014-MM-DD" description="Bug fixes and enhancements">
+      <action issue="LOG4J2-410" dev="rgoers" type="update" due-to="Ivlin Zeng">
+        SLf4JLogger is now Serializable.
+      </action>
       <action issue="LOG4J2-427" dev="rgoers" type="add" due-to="Alexander Reelsen">
         Add support for configuration via YAML.
       </action>