logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1431560 - in /logging/log4j/log4j2/trunk: core/src/main/java/org/apache/logging/log4j/core/appender/ core/src/main/java/org/apache/logging/log4j/core/impl/ core/src/main/java/org/apache/logging/log4j/core/layout/ core/src/main/java/org/apa...
Date Thu, 10 Jan 2013 19:05:58 GMT
Author: rgoers
Date: Thu Jan 10 19:05:58 2013
New Revision: 1431560

URL: http://svn.apache.org/viewvc?rev=1431560&view=rev
Log:
LOG4J2-142 - Serialized LogEvents were not reset in the output stream causing them to deserialize
incorrectly.

Added:
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/LogEventTest.java
Modified:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/AbstractOutputStreamAppender.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/SerializedLayout.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/net/TCPSocketManager.java
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/AsynchAppenderTest.java
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/SocketAppenderTest.java
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/layout/SerializedLayoutTest.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/AbstractOutputStreamAppender.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/AbstractOutputStreamAppender.java?rev=1431560&r1=1431559&r2=1431560&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/AbstractOutputStreamAppender.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/AbstractOutputStreamAppender.java
Thu Jan 10 19:05:58 2013
@@ -109,9 +109,12 @@ public abstract class AbstractOutputStre
     public void append(final LogEvent event) {
         readLock.lock();
         try {
-            manager.write(getLayout().toByteArray(event));
-            if (this.immediateFlush) {
-                manager.flush();
+            byte[] bytes = getLayout().toByteArray(event);
+            if (bytes.length > 0) {
+                manager.write(bytes);
+                if (this.immediateFlush) {
+                    manager.flush();
+                }
             }
         } catch (final AppenderRuntimeException ex) {
             error("Unable to write to stream " + manager.getName() + " for appender " + getName());

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java?rev=1431560&r1=1431559&r2=1431560&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java
Thu Jan 10 19:05:58 2013
@@ -20,8 +20,6 @@ import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.core.helpers.Loader;
 import org.apache.logging.log4j.status.StatusLogger;
 
-import java.io.PrintStream;
-import java.io.PrintWriter;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.net.URL;
@@ -47,10 +45,11 @@ public class ThrowableProxy extends Thro
     private static Method getSuppressed;
     private static Method addSuppressed;
 
-    private final Throwable throwable;
-    private final ThrowableProxy cause;
+    private final ThrowableProxy proxyCause;
     private int commonElementCount;
 
+    private final String name;
+
     private final StackTracePackageElement[] callerPackageData;
 
 
@@ -64,11 +63,13 @@ public class ThrowableProxy extends Thro
      * @param throwable The Throwable to wrap.
      */
     public ThrowableProxy(final Throwable throwable) {
-        this.throwable = throwable;
+        super(throwable.getMessage(), null);
+        this.name = throwable.getClass().getName();
         final Map<String, CacheEntry> map = new HashMap<String, CacheEntry>();
         final Stack<Class<?>> stack = getCurrentStack();
+        super.setStackTrace(throwable.getStackTrace());
         callerPackageData = resolvePackageData(stack, map, null, throwable.getStackTrace());
-        this.cause = (throwable.getCause() == null) ? null :
+        this.proxyCause = (throwable.getCause() == null) ? null :
             new ThrowableProxy(throwable, stack, map, throwable.getCause());
         setSuppressed(throwable);
     }
@@ -82,11 +83,13 @@ public class ThrowableProxy extends Thro
      * @param cause The Throwable to wrap.
      */
     private ThrowableProxy(final Throwable parent, final Stack<Class<?>> stack,
final Map<String, CacheEntry> map, final Throwable cause) {
-        this.throwable = cause;
+        super(cause.getMessage(), null);
+        this.name = cause.getClass().getName();
+        super.setStackTrace(cause.getStackTrace());
         callerPackageData = resolvePackageData(stack, map, parent.getStackTrace(), cause.getStackTrace());
-        this.cause = (throwable.getCause() == null) ? null :
-            new ThrowableProxy(parent, stack, map, throwable.getCause());
-        setSuppressed(throwable);
+        this.proxyCause = (cause.getCause() == null) ? null :
+            new ThrowableProxy(parent, stack, map, cause.getCause());
+        setSuppressed(cause);
     }
 
 
@@ -96,18 +99,8 @@ public class ThrowableProxy extends Thro
     }
 
     @Override
-    public String getMessage() {
-        return throwable.getMessage();
-    }
-
-    @Override
-    public String getLocalizedMessage() {
-        return throwable.getLocalizedMessage();
-    }
-
-    @Override
     public Throwable getCause() {
-        return cause;
+        return proxyCause;
     }
 
     @Override
@@ -117,22 +110,8 @@ public class ThrowableProxy extends Thro
 
     @Override
     public String toString() {
-        return throwable.toString();
-    }
-
-    @Override
-    public void printStackTrace() {
-        throwable.printStackTrace();
-    }
-
-    @Override
-    public void printStackTrace(final PrintStream printStream) {
-        throwable.printStackTrace(printStream);
-    }
-
-    @Override
-    public void printStackTrace(final PrintWriter printWriter) {
-        throwable.printStackTrace(printWriter);
+        String msg = getMessage();
+        return msg != null ? name + ": " + msg : name;
     }
 
     @Override
@@ -140,10 +119,11 @@ public class ThrowableProxy extends Thro
         return this;
     }
 
+    /*
     @Override
     public StackTraceElement[] getStackTrace() {
-        return throwable.getStackTrace();
-    }
+        return callerData;
+    } */
 
     /**
      * Format the Throwable that is the cause of this Throwable.
@@ -160,13 +140,13 @@ public class ThrowableProxy extends Thro
      */
     public String getRootCauseStackTrace(final List<String> packages) {
         final StringBuilder sb = new StringBuilder();
-        if (cause != null) {
-            formatWrapper(sb, cause);
+        if (proxyCause != null) {
+            formatWrapper(sb, proxyCause);
             sb.append("Wrapped by: ");
         }
-        sb.append(throwable.toString());
+        sb.append(toString());
         sb.append("\n");
-        formatElements(sb, 0, throwable.getStackTrace(), callerPackageData, packages);
+        formatElements(sb, 0, getStackTrace(), callerPackageData, packages);
         return sb.toString();
     }
 
@@ -188,7 +168,7 @@ public class ThrowableProxy extends Thro
     public void formatWrapper(final StringBuilder sb, final ThrowableProxy cause, final List<String>
packages) {
         final Throwable caused = cause.getCause();
         if (caused != null) {
-            formatWrapper(sb, cause.cause);
+            formatWrapper(sb, cause.proxyCause);
             sb.append("Wrapped by: ");
         }
         sb.append(cause).append("\n");
@@ -209,11 +189,15 @@ public class ThrowableProxy extends Thro
      * @return The formatted stack trace including packaging information.
      */
     public String getExtendedStackTrace(final List<String> packages) {
-        final StringBuilder sb = new StringBuilder(throwable.toString());
+        final StringBuilder sb = new StringBuilder(name);
+        String msg = getMessage();
+        if (msg != null) {
+            sb.append(": ").append(getMessage());
+        }
         sb.append("\n");
-        formatElements(sb, 0, throwable.getStackTrace(), callerPackageData, packages);
-        if (cause != null) {
-            formatCause(sb, cause, packages);
+        formatElements(sb, 0, getStackTrace(), callerPackageData, packages);
+        if (proxyCause != null) {
+            formatCause(sb, proxyCause, packages);
         }
         return sb.toString();
     }
@@ -238,7 +222,7 @@ public class ThrowableProxy extends Thro
         sb.append("Caused by: ").append(cause).append("\n");
         formatElements(sb, cause.commonElementCount, cause.getStackTrace(), cause.callerPackageData,
packages);
         if (cause.getCause() != null) {
-            formatCause(sb, cause.cause, packages);
+            formatCause(sb, cause.proxyCause, packages);
         }
     }
 

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/SerializedLayout.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/SerializedLayout.java?rev=1431560&r1=1431559&r2=1431560&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/SerializedLayout.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/SerializedLayout.java
Thu Jan 10 19:05:58 2013
@@ -59,6 +59,7 @@ public final class SerializedLayout exte
             final ObjectOutputStream oos = new PrivateObjectOutputStream(baos);
             try {
                 oos.writeObject(event);
+                oos.reset();
             } finally {
                 oos.close();
             }

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/net/TCPSocketManager.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/net/TCPSocketManager.java?rev=1431560&r1=1431559&r2=1431560&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/net/TCPSocketManager.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/net/TCPSocketManager.java
Thu Jan 10 19:05:58 2013
@@ -109,7 +109,6 @@ public class TCPSocketManager extends Ab
         }
         try {
             getOutputStream().write(bytes, offset, length);
-            socket.setSendBufferSize(length);
         } catch (final IOException ex) {
             if (retry && connector == null) {
                 connector = new Reconnector(this);

Added: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/LogEventTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/LogEventTest.java?rev=1431560&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/LogEventTest.java
(added)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/LogEventTest.java
Thu Jan 10 19:05:58 2013
@@ -0,0 +1,67 @@
+/*
+ * 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.log4j.core;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LoggingException;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.message.SimpleMessage;
+import org.junit.Test;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
+import static org.junit.Assert.fail;
+
+/**
+ *
+ */
+public class LogEventTest {
+
+    @Test
+    public void testSerialization() throws Exception {
+        final LogEvent event1 = new Log4jLogEvent(this.getClass().getName(), null, "org.apache.logging.log4j.core.Logger",
+            Level.INFO, new SimpleMessage("Hello, world!"), null);
+        Exception parent = new IllegalStateException("Test");
+        Throwable child = new LoggingException("This is a test", parent);
+        final LogEvent event2 = new Log4jLogEvent(this.getClass().getName(), null, "org.apache.logging.log4j.core.Logger",
+            Level.INFO, new SimpleMessage("Hello, world!"), child);
+
+        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        final ObjectOutputStream oos = new ObjectOutputStream(baos);
+        oos.writeObject(event1);
+        oos.writeObject(event2);
+
+        final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+        final ObjectInputStream ois = new ObjectInputStream(bais);
+        LogEvent returned;
+        try {
+            returned = (LogEvent) ois.readObject();
+        } catch (final IOException ioe) {
+            fail("Exception processing event1");
+        }
+        try {
+            returned = (LogEvent) ois.readObject();
+        } catch (final IOException ioe) {
+            fail("Exception processing event2");
+        }
+    }
+}

Modified: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/AsynchAppenderTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/AsynchAppenderTest.java?rev=1431560&r1=1431559&r2=1431560&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/AsynchAppenderTest.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/AsynchAppenderTest.java
Thu Jan 10 19:05:58 2013
@@ -18,12 +18,14 @@ package org.apache.logging.log4j.core.ap
 
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.LoggingException;
 import org.apache.logging.log4j.core.Appender;
 import org.apache.logging.log4j.core.LoggerContext;
 import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.XMLConfigurationFactory;
 import org.apache.logging.log4j.status.StatusLogger;
 import org.apache.logging.log4j.test.appender.ListAppender;
+import org.junit.After;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -62,6 +64,11 @@ public class AsynchAppenderTest {
         StatusLogger.getLogger().reset();
     }
 
+    @After
+    public void after() {
+        app.clear();
+    }
+
     @Test
     public void rewriteTest() throws Exception {
         final Logger logger = LogManager.getLogger(AsynchAppender.class);
@@ -78,4 +85,18 @@ public class AsynchAppenderTest {
         expected = AsynchAppenderTest.class.getName() + " rewriteTest Hello world!";
         assertTrue("Expected " + expected + ", Actual " + msg, expected.equals(msg));
     }
+
+    @Test
+    public void testException() throws Exception {
+        final Logger logger = LogManager.getLogger(AsynchAppender.class);
+        Exception parent = new IllegalStateException("Test");
+        Throwable child = new LoggingException("This is a test", parent);
+        logger.error("This is a test", child);
+        Thread.sleep(100);
+        final List<String> list = app.getMessages();
+        assertNotNull("No events generated", list);
+        assertTrue("Incorrect number of events. Expected 1, got " + list.size(), list.size()
== 1);
+        String msg = list.get(0);
+        assertTrue("No parent exception", msg.contains("java.lang.IllegalStateException"));
+    }
 }

Modified: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/SocketAppenderTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/SocketAppenderTest.java?rev=1431560&r1=1431559&r2=1431560&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/SocketAppenderTest.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/SocketAppenderTest.java
Thu Jan 10 19:05:58 2013
@@ -18,6 +18,7 @@ package org.apache.logging.log4j.core.ap
 
 import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.LoggingException;
 import org.apache.logging.log4j.core.Appender;
 import org.apache.logging.log4j.core.LogEvent;
 import org.apache.logging.log4j.core.Logger;
@@ -76,6 +77,7 @@ public class SocketAppenderTest {
     public static void cleanupClass() {
         tcp.shutdown();
         udp.shutdown();
+        list.clear();
     }
 
     @After
@@ -88,6 +90,7 @@ public class SocketAppenderTest {
         }
         tcpCount = 0;
         udpCount = 0;
+        list.clear();
     }
 
     @Test
@@ -102,10 +105,19 @@ public class SocketAppenderTest {
         root.setAdditive(false);
         root.setLevel(Level.DEBUG);
         root.debug("This is a test message");
-        final LogEvent event = list.poll(3, TimeUnit.SECONDS);
+        Exception parent = new IllegalStateException("Test");
+        Throwable child = new LoggingException("This is a test");
+        root.error("Throwing an exception", child);
+        root.debug("This is another test message");
+        Thread.sleep(250);
+        LogEvent event = list.poll(3, TimeUnit.SECONDS);
         assertNotNull("No event retrieved", event);
         assertTrue("Incorrect event", event.getMessage().getFormattedMessage().equals("This
is a test message"));
         assertTrue("Message not delivered via TCP", tcpCount > 0);
+        event = list.poll(3, TimeUnit.SECONDS);
+        assertNotNull("No event retrieved", event);
+        assertTrue("Incorrect event", event.getMessage().getFormattedMessage().equals("Throwing
an exception"));
+        assertTrue("Message not delivered via TCP", tcpCount > 1);
     }
 
 
@@ -120,10 +132,10 @@ public class SocketAppenderTest {
         root.addAppender(appender);
         root.setAdditive(false);
         root.setLevel(Level.DEBUG);
-        root.debug("This is a test message");
+        root.debug("This is a udp message");
         final LogEvent event = list.poll(3, TimeUnit.SECONDS);
         assertNotNull("No event retrieved", event);
-        assertTrue("Incorrect event", event.getMessage().getFormattedMessage().equals("This
is a test message"));
+        assertTrue("Incorrect event", event.getMessage().getFormattedMessage().equals("This
is a udp message"));
         assertTrue("Message not delivered via UDP", udpCount > 0);
     }
 

Modified: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/layout/SerializedLayoutTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/layout/SerializedLayoutTest.java?rev=1431560&r1=1431559&r2=1431560&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/layout/SerializedLayoutTest.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/layout/SerializedLayoutTest.java
Thu Jan 10 19:05:58 2013
@@ -18,6 +18,7 @@ package org.apache.logging.log4j.core.la
 
 import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.LoggingException;
 import org.apache.logging.log4j.ThreadContext;
 import org.apache.logging.log4j.core.BasicConfigurationFactory;
 import org.apache.logging.log4j.core.LogEvent;
@@ -64,7 +65,8 @@ public class SerializedLayoutTest {
         "Logger=root Level=DEBUG Message=starting mdc pattern test",
         "Logger=root Level=DEBUG Message=empty mdc",
         "Logger=root Level=DEBUG Message=filled mdc",
-        "Logger=root Level=ERROR Message=finished mdc pattern test"
+        "Logger=root Level=ERROR Message=finished mdc pattern test",
+        "Logger=root Level=ERROR Message=Throwing an exception"
     };
 
 
@@ -98,6 +100,11 @@ public class SerializedLayoutTest {
 
         root.error("finished mdc pattern test", new NullPointerException("test"));
 
+        Exception parent = new IllegalStateException("Test");
+        Throwable child = new LoggingException("This is a test", parent);
+
+        root.error("Throwing an exception", child);
+
         appender.stop();
 
         final List<byte[]> data = appender.getData();

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1431560&r1=1431559&r2=1431560&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Thu Jan 10 19:05:58 2013
@@ -23,6 +23,9 @@
 
   <body>
     <release version="2.0-beta4" date="TBD" description="Bug fixes and enhancements">
+      <action issue="LOG4J2-142" dev="rgoers" type="fix">
+        Serialized LogEvents were not reset in the output stream causing them to deserialize
incorrectly.
+      </action>
       <action issue="LOG4J2-139" dev="rgoers" type="fix">
         Fix null pointer exception in SocketAppender if no protocol is specified. The protocol
will default
         to TCP for the SocketAppender and UDP for the SyslogAppender.



Mime
View raw message