LOG4J2-1527 Prevent NPE in RingBufferLogEvent.getFormattedMessage() when used in web applications.
This closes #39 (https://github.com/apache/logging-log4j2/pull/39 ) Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/147f78c4 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/147f78c4 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/147f78c4 Branch: refs/heads/LOG4J2-1528 Commit: 147f78c45e181d78778a710d24510c9b03d97bc7 Parents: 6a23301 Author: rpopma <[email protected]> Authored: Sat Aug 20 09:44:20 2016 +0900 Committer: rpopma <[email protected]> Committed: Sat Aug 20 09:44:20 2016 +0900 ---------------------------------------------------------------------- .../log4j/core/async/RingBufferLogEvent.java | 4 +++- .../log4j/core/async/RingBufferLogEventTest.java | 18 ++++++++++++++---- src/changes/changes.xml | 4 ++++ 3 files changed, 21 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/147f78c4/log4j-core/src/main/java/org/apache/logging/log4j/core/async/RingBufferLogEvent.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/RingBufferLogEvent.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/RingBufferLogEvent.java index c4de9d4..76b00a1 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/RingBufferLogEvent.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/RingBufferLogEvent.java @@ -210,7 +210,9 @@ public class RingBufferLogEvent implements LogEvent, ReusableMessage, CharSequen */ @Override public String getFormattedMessage() { - return messageText.toString(); + return messageText != null // LOG4J2-1527: may be null in web apps + ? messageText.toString() // note: please keep below "redundant" braces for readability + : (message == null ? null : message.getFormattedMessage()); } /** http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/147f78c4/log4j-core/src/test/java/org/apache/logging/log4j/core/async/RingBufferLogEventTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/RingBufferLogEventTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/RingBufferLogEventTest.java index ec3a874..5c7f467 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/RingBufferLogEventTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/RingBufferLogEventTest.java @@ -121,11 +121,11 @@ public class RingBufferLogEventTest { final long nanoTime = 1; evt.setValues(null, loggerName, marker, fqcn, level, data, t, map, contextStack, -1, threadName, -1, location, currentTimeMillis, nanoTime); - + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ObjectOutputStream out = new ObjectOutputStream(baos); out.writeObject(evt); - + final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())); final RingBufferLogEvent other = (RingBufferLogEvent) in.readObject(); assertEquals(loggerName, other.getLoggerName()); @@ -141,7 +141,7 @@ public class RingBufferLogEventTest { assertEquals(location, other.getSource()); assertEquals(currentTimeMillis, other.getTimeMillis()); } - + @Test public void testCreateMementoReturnsCopy() { final RingBufferLogEvent evt = new RingBufferLogEvent(); @@ -160,7 +160,7 @@ public class RingBufferLogEventTest { final long nanoTime = 1; evt.setValues(null, loggerName, marker, fqcn, level, data, t, map, contextStack, -1, threadName, -1, location, currentTimeMillis, nanoTime); - + final LogEvent actual = evt.createMemento(); assertEquals(evt.getLoggerName(), actual.getLoggerName()); assertEquals(evt.getMarker(), actual.getMarker()); @@ -175,4 +175,14 @@ public class RingBufferLogEventTest { assertEquals(evt.getSource(), actual.getSource()); assertEquals(evt.getThrownProxy(), actual.getThrownProxy()); } + + @Test + public void testMessageTextNeverThrowsNpe() { + final RingBufferLogEvent evt = new RingBufferLogEvent(); + try { + evt.getFormattedMessage(); + } catch (NullPointerException e) { + fail("the messageText field was not set"); + } + } } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/147f78c4/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index be283c7..1ea373c 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -24,6 +24,10 @@ </properties> <body> <release version="2.7" date="2016-MM-DD" description="GA Release 2.7"> + + <action issue="LOG4J2-1527" dev="rpopma" type="fix" due-to="Jose Leon"> + Prevent NPE in RingBufferLogEvent.getFormattedMessage() when used in web applications. + </action> <action issue="LOG4J2-905" dev="ggregory" type="fix" due-to="Gary Gregory, Moritz Löser"> Ability to disable (date) lookup completely, compatibility issues with other libraries like Camel. </action>
