You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by vy...@apache.org on 2022/04/15 09:09:23 UTC

[logging-log4j2] branch release-2.x updated: LOG4J2-3477 Add the missing context stack to JsonLayout template. (#819)

This is an automated email from the ASF dual-hosted git repository.

vy pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/release-2.x by this push:
     new 041648a6a3 LOG4J2-3477 Add the missing context stack to JsonLayout template. (#819)
041648a6a3 is described below

commit 041648a6a3c74abad6a90dee908d1a5383551d76
Author: Volkan Yazıcı <vo...@yazi.ci>
AuthorDate: Fri Apr 15 11:04:12 2022 +0200

    LOG4J2-3477 Add the missing context stack to JsonLayout template. (#819)
---
 .../src/main/resources/JsonLayout.json                       |  4 ++++
 .../logging/log4j/layout/template/json/JsonLayoutTest.java   | 12 ++++++++----
 src/changes/changes.xml                                      |  3 +++
 3 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/log4j-layout-template-json/src/main/resources/JsonLayout.json b/log4j-layout-template-json/src/main/resources/JsonLayout.json
index 503e2cd875..6f21011cf3 100644
--- a/log4j-layout-template-json/src/main/resources/JsonLayout.json
+++ b/log4j-layout-template-json/src/main/resources/JsonLayout.json
@@ -44,6 +44,10 @@
       "field": "stackTrace"
     }
   },
+  "contextMap": {
+    "$resolver": "mdc",
+    "stringified": true
+  },
   "contextStack": {
     "$resolver": "ndc"
   },
diff --git a/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonLayoutTest.java b/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonLayoutTest.java
index 938b275659..cb3a1d1586 100644
--- a/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonLayoutTest.java
+++ b/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/JsonLayoutTest.java
@@ -42,6 +42,7 @@ class JsonLayoutTest {
     private static final JsonLayout JSON_LAYOUT = JsonLayout
             .newBuilder()
             .setConfiguration(CONFIGURATION)
+            .setProperties(true)
             .build();
 
     @Test
@@ -65,12 +66,15 @@ class JsonLayoutTest {
     private static void test(final LogEvent logEvent) {
         final Map<String, Object> jsonTemplateLayoutMap = renderUsingJsonTemplateLayout(logEvent);
         final Map<String, Object> jsonLayoutMap = renderUsingJsonLayout(logEvent);
-        // JsonLayout blindly serializes the Throwable as a POJO, this is,
-        // to say the least, quite wrong, and I ain't gonna try to emulate
-        // this behaviour in JsonTemplateLayout. Hence, discarding the "thrown"
-        // field.
+        // `JsonLayout` blindly serializes the `Throwable` as a POJO, this is, to say the least, quite wrong, and I ain't going to try to emulate this behaviour in `JsonTemplateLayout`.
+        // Hence, discarding the "thrown" field.
         jsonTemplateLayoutMap.remove("thrown");
         jsonLayoutMap.remove("thrown");
+        // When the log event doesn't have any MDC, `JsonLayout` still emits an empty `contextMap` field, whereas `JsonTemplateLayout` totally skips it.
+        // Removing `contextMap` field to avoid discrepancies when there is no MDC to render.
+        if (logEvent.getContextData().isEmpty()) {
+            jsonLayoutMap.remove("contextMap");
+        }
         Assertions.assertThat(jsonTemplateLayoutMap).isEqualTo(jsonLayoutMap);
     }
 
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 7f7fdc296f..c8307a10bf 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -31,6 +31,9 @@
     -->
     <release version="2.17.3" date="2022-TBD" description="GA Release 2.18.0">
       <!-- FIXES -->
+      <action issue="LOG4J2-3477" dev="vy" type="fix" due-to="filipc">
+        Add the missing context stack to JsonLayout template.
+      </action>
       <action issue="LOG4J2-3393" dev="vy" type="fix">
         Improve JsonTemplateLayout performance.
       </action>