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:31:20 UTC

[logging-log4j2] branch master 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 master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


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

commit 53009e0f31c78e4220f1e5ec33854b1bd65833fa
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 1c9969244a..5eb24e5196 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 6b77862851..7aca637841 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -193,6 +193,9 @@
     </release>
     <release version="2.17.3" date="20YY-MM-DD" description="GA Release 2.17.3">
       <!-- FIXES -->
+      <action issue="LOG4J2-3477" dev="vy" type="fix" due-to="filipc">
+        Add the missing context stack to JsonLayout template.
+      </action>
       <action issue="LOG4J2-3458" dev="ggregory" type="fix" due-to="Gary Gregory">
         LocalizedMessage logs misleading errors on the console.
       </action>