Description
In a similar vein to LOG4J2-914, I also am attempting to use log4j as a daemon log server. The fix for LOG4J2-914 only solved the NPE problem for one dimensional exceptions. Nested exceptions also cause an NPE in the current implementation. Here is a test/patch diff for the bug:
--- .../org/apache/logging/log4j/core/impl/ThrowableProxy.java | 2 +- .../org/apache/logging/log4j/core/impl/ThrowableProxyTest.java | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java index 67d55ec..307de58 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java @@ -207,7 +207,7 @@ public class ThrowableProxy implements Serializable { return; } sb.append("Caused by: ").append(cause).append(EOL); - this.formatElements(sb, cause.commonElementCount, cause.getThrowable().getStackTrace(), + this.formatElements(sb, cause.commonElementCount, cause.getStackTrace(), cause.extendedStackTrace, ignorePackages); this.formatCause(sb, cause.causeProxy, ignorePackages); } diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyTest.java index 7019aa2..6eb5dbc 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyTest.java @@ -146,6 +146,16 @@ public class ThrowableProxyTest { assertEquals(proxy.getExtendedStackTraceAsString(), proxy2.getExtendedStackTraceAsString()); } + + @Test + public void testSerialization_getExtendedStackTraceAsStringWithNestedThrowable() throws Exception { + final Throwable throwable = new RuntimeException(new IllegalArgumentException("This is a test")); + final ThrowableProxy proxy = new ThrowableProxy(throwable); + final byte[] binary = serialize(proxy); + final ThrowableProxy proxy2 = deserialize(binary); + + assertEquals(proxy.getExtendedStackTraceAsString(), proxy2.getExtendedStackTraceAsString()); + } @Test public void testSerializationWithUnknownThrowable() throws Exception { --