You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ck...@apache.org on 2018/07/03 18:31:02 UTC
logging-log4j2 git commit: LOG4J2-2312 LOG4J2-2341 Fix jackson layout
with async loggers
Repository: logging-log4j2
Updated Branches:
refs/heads/master d6ba15d30 -> 34cc74570
LOG4J2-2312 LOG4J2-2341 Fix jackson layout with async loggers
AsyncLoggerContextSelector RingBufferLogEvents were not properly
handled by the jackson layout. Currently the jackson layout
implementation requires Log4jLogEvent instances, so we must
convert all other LogEvent implementations into these.
This closes #187
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/34cc7457
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/34cc7457
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/34cc7457
Branch: refs/heads/master
Commit: 34cc745705b0c33d79ef326032edf9cb212fc26f
Parents: d6ba15d
Author: Carter Kozak <ck...@apache.org>
Authored: Tue Jul 3 11:13:34 2018 -0400
Committer: Carter Kozak <ck...@apache.org>
Committed: Tue Jul 3 14:30:30 2018 -0400
----------------------------------------------------------------------
log4j-layout-jackson-json/pom.xml | 5 +++
.../jackson/json/layout/JsonLayoutTest.java | 37 ++++++++++++++++++++
.../log4j/jackson/AbstractJacksonLayout.java | 4 +--
.../jackson/layout/AbstractJacksonLayout.java | 4 +--
src/changes/changes.xml | 6 ++++
5 files changed, 52 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/34cc7457/log4j-layout-jackson-json/pom.xml
----------------------------------------------------------------------
diff --git a/log4j-layout-jackson-json/pom.xml b/log4j-layout-jackson-json/pom.xml
index 962f4ac..6c97168 100644
--- a/log4j-layout-jackson-json/pom.xml
+++ b/log4j-layout-jackson-json/pom.xml
@@ -55,6 +55,11 @@
<type>test-jar</type>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>com.lmax</groupId>
+ <artifactId>disruptor</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/34cc7457/log4j-layout-jackson-json/src/test/java/org/apache/logging/log4j/jackson/json/layout/JsonLayoutTest.java
----------------------------------------------------------------------
diff --git a/log4j-layout-jackson-json/src/test/java/org/apache/logging/log4j/jackson/json/layout/JsonLayoutTest.java b/log4j-layout-jackson-json/src/test/java/org/apache/logging/log4j/jackson/json/layout/JsonLayoutTest.java
index 6aa5f73..d477d23 100644
--- a/log4j-layout-jackson-json/src/test/java/org/apache/logging/log4j/jackson/json/layout/JsonLayoutTest.java
+++ b/log4j-layout-jackson-json/src/test/java/org/apache/logging/log4j/jackson/json/layout/JsonLayoutTest.java
@@ -16,6 +16,8 @@
*/
package org.apache.logging.log4j.jackson.json.layout;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
@@ -33,12 +35,15 @@ import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.BasicConfigurationFactory;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.async.RingBufferLogEvent;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.core.impl.MutableLogEvent;
import org.apache.logging.log4j.core.layout.LogEventFixtures;
import org.apache.logging.log4j.core.lookup.JavaLookup;
+import org.apache.logging.log4j.core.time.internal.DummyNanoClock;
+import org.apache.logging.log4j.core.time.internal.SystemClock;
import org.apache.logging.log4j.core.util.KeyValuePair;
import org.apache.logging.log4j.jackson.AbstractJacksonLayout;
import org.apache.logging.log4j.jackson.json.Log4jJsonObjectMapper;
@@ -49,6 +54,7 @@ import org.apache.logging.log4j.message.ReusableMessageFactory;
import org.apache.logging.log4j.message.SimpleMessage;
import org.apache.logging.log4j.spi.AbstractLogger;
import org.apache.logging.log4j.test.appender.ListAppender;
+import org.apache.logging.log4j.util.SortedArrayStringMap;
import org.apache.logging.log4j.util.Strings;
import org.junit.AfterClass;
import org.junit.Assert;
@@ -505,6 +511,37 @@ public class JsonLayoutTest {
}
}
+ // Test for LOG4J2-2312 LOG4J2-2341
+ @Test
+ public void testLayoutRingBufferEventReusableMessageWithCurlyBraces() throws Exception {
+ final boolean propertiesAsList = false;
+ final AbstractJacksonLayout layout = JsonLayout.newBuilder()
+ .setLocationInfo(false)
+ .setProperties(false)
+ .setPropertiesAsList(propertiesAsList)
+ .setComplete(false)
+ .setCompact(true)
+ .setEventEol(false)
+ .setCharset(StandardCharsets.UTF_8)
+ .setIncludeStacktrace(true)
+ .build();
+ Message message = ReusableMessageFactory.INSTANCE.newMessage("Testing {}", new TestObj());
+ try {
+ RingBufferLogEvent ringBufferEvent = new RingBufferLogEvent();
+ ringBufferEvent.setValues(
+ null, "a.B", null, "f.q.c.n", Level.DEBUG, message,
+ null, new SortedArrayStringMap(), ThreadContext.EMPTY_STACK, 1L,
+ "threadName", 1, null, new SystemClock(), new DummyNanoClock());
+ final String str = layout.toSerializable(ringBufferEvent);
+ final String expectedMessage = "Testing " + TestObj.TO_STRING_VALUE;
+ assertThat(str, containsString("\"message\":\"" + expectedMessage + '"'));
+ final Log4jLogEvent actual = new Log4jJsonObjectMapper(propertiesAsList, true, false, false).readValue(str, Log4jLogEvent.class);
+ assertEquals(expectedMessage, actual.getMessage().getFormattedMessage());
+ } finally {
+ ReusableMessageFactory.release(message);
+ }
+ }
+
static class TestObj {
static final String TO_STRING_VALUE = "This is my toString {} with curly braces";
@Override
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/34cc7457/log4j-layout-jackson/src/main/java/org/apache/logging/log4j/jackson/AbstractJacksonLayout.java
----------------------------------------------------------------------
diff --git a/log4j-layout-jackson/src/main/java/org/apache/logging/log4j/jackson/AbstractJacksonLayout.java b/log4j-layout-jackson/src/main/java/org/apache/logging/log4j/jackson/AbstractJacksonLayout.java
index da66b9d..2e35b01 100644
--- a/log4j-layout-jackson/src/main/java/org/apache/logging/log4j/jackson/AbstractJacksonLayout.java
+++ b/log4j-layout-jackson/src/main/java/org/apache/logging/log4j/jackson/AbstractJacksonLayout.java
@@ -26,7 +26,7 @@ import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
-import org.apache.logging.log4j.core.impl.MutableLogEvent;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.core.layout.AbstractStringLayout;
import org.apache.logging.log4j.core.lookup.StrSubstitutor;
import org.apache.logging.log4j.core.util.KeyValuePair;
@@ -219,7 +219,7 @@ public abstract class AbstractJacksonLayout extends AbstractStringLayout {
// TODO Jackson-based layouts have certain filters set up for Log4jLogEvent.
// TODO Need to set up the same filters for MutableLogEvent but don't know how...
// This is a workaround.
- return event instanceof MutableLogEvent ? ((MutableLogEvent) event).createMemento() : event;
+ return event instanceof Log4jLogEvent ? event : Log4jLogEvent.createMemento(event);
}
private static ResolvableKeyValuePair[] prepareAdditionalFields(final Configuration config,
final KeyValuePair[] additionalFields) {
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/34cc7457/log4j-layout-jackson/src/main/java/org/apache/logging/log4j/jackson/layout/AbstractJacksonLayout.java
----------------------------------------------------------------------
diff --git a/log4j-layout-jackson/src/main/java/org/apache/logging/log4j/jackson/layout/AbstractJacksonLayout.java b/log4j-layout-jackson/src/main/java/org/apache/logging/log4j/jackson/layout/AbstractJacksonLayout.java
index a468c1e..175a470 100644
--- a/log4j-layout-jackson/src/main/java/org/apache/logging/log4j/jackson/layout/AbstractJacksonLayout.java
+++ b/log4j-layout-jackson/src/main/java/org/apache/logging/log4j/jackson/layout/AbstractJacksonLayout.java
@@ -44,7 +44,7 @@ import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
-import org.apache.logging.log4j.core.impl.MutableLogEvent;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.core.layout.AbstractStringLayout;
import org.apache.logging.log4j.core.lookup.StrSubstitutor;
import org.apache.logging.log4j.core.util.KeyValuePair;
@@ -245,7 +245,7 @@ abstract class AbstractJacksonLayout extends AbstractStringLayout {
// TODO Jackson-based layouts have certain filters set up for Log4jLogEvent.
// TODO Need to set up the same filters for MutableLogEvent but don't know how...
// This is a workaround.
- return event instanceof MutableLogEvent ? ((MutableLogEvent) event).createMemento() : event;
+ return event instanceof Log4jLogEvent ? event : Log4jLogEvent.createMemento(event);
}
private static ResolvableKeyValuePair[] prepareAdditionalFields(final Configuration config,
final KeyValuePair[] additionalFields) {
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/34cc7457/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 439fdc6..da134c9 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -197,6 +197,9 @@
<action issue="LOG4J2-2362" dev="ckozak" type="fix">
Fixed a memory leak in which ReusableObjectMessage would hold a reference to the most recently logged object.
</action>
+ <action issue="LOG4J2-2312" dev="ckozak" type="fix">
+ Jackson layouts used with AsyncLoggerContextSelector output the expected format rather than only a json string of the message text.
+ </action>
</release>
<release version="2.11.1" date="2018-MM-DD" description="GA Release 2.11.1">
<action issue="LOG4J2-1721" dev="rgoers" type="update" due-to="Phokham Nonava">
@@ -304,6 +307,9 @@
<action issue="LOG4J2-2362" dev="ckozak" type="fix">
Fixed a memory leak in which ReusableObjectMessage would hold a reference to the most recently logged object.
</action>
+ <action issue="LOG4J2-2312" dev="ckozak" type="fix">
+ Jackson layouts used with AsyncLoggerContextSelector output the expected format rather than only a json string of the message text.
+ </action>
</release>
<release version="2.11.0" date="2018-xx-xx" description="GA Release 2.11.0">
<action issue="LOG4J2-2104" dev="rgoers" type="fix">