You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2021/03/21 00:16:40 UTC

[logging-log4j2] branch master updated: LOG4J2-3049 - Allow MapMessage and ThreadContext attributes to be prefixed

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

rgoers 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 62804d5  LOG4J2-3049 - Allow MapMessage and ThreadContext attributes to be prefixed
62804d5 is described below

commit 62804d5750714310a05b6e241de9905199301d25
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Sat Mar 20 17:15:56 2021 -0700

    LOG4J2-3049 - Allow MapMessage and ThreadContext attributes to be prefixed
---
 .../logging/log4j/core/layout/GelfLayout.java      | 46 ++++++++++++++++++----
 .../logging/log4j/core/layout/GelfLayout3Test.java | 24 +++++------
 log4j-core/src/test/resources/GelfLayout3Test.xml  |  2 +-
 src/changes/changes.xml                            |  3 ++
 src/site/asciidoc/manual/layouts.adoc              |  9 ++++-
 5 files changed, 64 insertions(+), 20 deletions(-)

diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/GelfLayout.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/GelfLayout.java
index c035dcc..fb52cf1 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/GelfLayout.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/GelfLayout.java
@@ -156,6 +156,12 @@ public final class GelfLayout extends AbstractStringLayout {
         @PluginBuilderAttribute
         private String messagePattern = null;
 
+        @PluginBuilderAttribute
+        private String threadContextPrefix = "";
+
+        @PluginBuilderAttribute
+        private String mapPrefix = "";
+
         @PluginElement("PatternSelector")
         private PatternSelector patternSelector = null;
 
@@ -188,7 +194,7 @@ public final class GelfLayout extends AbstractStringLayout {
             }
             return new GelfLayout(getConfiguration(), host, additionalFields, compressionType, compressionThreshold,
                     includeStacktrace, includeThreadContext, includeMapMessage, includeNullDelimiter,
-                    includeNewLineDelimiter, mdcChecker, mapChecker, patternLayout);
+                    includeNewLineDelimiter, mdcChecker, mapChecker, patternLayout, threadContextPrefix, mapPrefix);
         }
 
         private ListChecker createChecker(String excludes, String includes) {
@@ -400,13 +406,37 @@ public final class GelfLayout extends AbstractStringLayout {
             this.mapMessageExcludes = mapMessageExcludes;
             return asBuilder();
         }
+
+        /**
+         * The String to prefix the ThreadContext attributes.
+         * @param prefix The prefix value. Null values will be ignored.
+         * @return this builder.
+         */
+        public B setThreadContextPrefix(final String prefix) {
+            if (prefix != null) {
+                this.threadContextPrefix = prefix;
+            }
+            return asBuilder();
+        }
+
+        /**
+         * The String to prefix the MapMessage attributes.
+         * @param prefix The prefix value. Null values will be ignored.
+         * @return this builder.
+         */
+        public B setMapPrefix(final String prefix) {
+            if (prefix != null) {
+                this.mapPrefix = prefix;
+            }
+            return asBuilder();
+        }
     }
 
     private GelfLayout(final Configuration config, final String host, final KeyValuePair[] additionalFields,
             final CompressionType compressionType, final int compressionThreshold, final boolean includeStacktrace,
             final boolean includeThreadContext, final boolean includeMapMessage, final boolean includeNullDelimiter,
             final boolean includeNewLineDelimiter, final ListChecker mdcChecker, final ListChecker mapChecker,
-            final PatternLayout patternLayout) {
+            final PatternLayout patternLayout, final String mdcPrefix, final String mapPrefix) {
         super(config, StandardCharsets.UTF_8, null, null);
         this.host = host != null ? host : NetUtils.getLocalHostname();
         this.additionalFields = additionalFields != null ? additionalFields : new KeyValuePair[0];
@@ -427,8 +457,8 @@ public final class GelfLayout extends AbstractStringLayout {
         if (includeNullDelimiter && compressionType != CompressionType.OFF) {
             throw new IllegalArgumentException("null delimiter cannot be used with compression");
         }
-        this.mdcWriter = new FieldWriter(mdcChecker);
-        this.mapWriter = new FieldWriter(mapChecker);
+        this.mdcWriter = new FieldWriter(mdcChecker, mdcPrefix);
+        this.mapWriter = new FieldWriter(mapChecker, mapPrefix);
         this.layout = patternLayout;
     }
 
@@ -603,16 +633,18 @@ public final class GelfLayout extends AbstractStringLayout {
 
     private static class FieldWriter implements TriConsumer<String, Object, StringBuilder> {
         private final ListChecker checker;
+        private final String prefix;
 
-        FieldWriter(ListChecker checker) {
+        FieldWriter(ListChecker checker, String prefix) {
             this.checker = checker;
+            this.prefix = prefix;
         }
 
         @Override
         public void accept(final String key, final Object value, final StringBuilder stringBuilder) {
             if (checker.check(key)) {
                 stringBuilder.append(QU);
-                JsonUtils.quoteAsString(key, stringBuilder);
+                JsonUtils.quoteAsString(prefix + key, stringBuilder);
                 stringBuilder.append("\":\"");
                 JsonUtils.quoteAsString(toNullSafeString(String.valueOf(value)), stringBuilder);
                 stringBuilder.append(QC);
@@ -622,7 +654,7 @@ public final class GelfLayout extends AbstractStringLayout {
         public ListChecker getChecker() {
             return checker;
         }
-    };
+    }
 
     private static final ThreadLocal<StringBuilder> messageStringBuilder = new ThreadLocal<>();
 
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/GelfLayout3Test.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/GelfLayout3Test.java
index f2f15f8..7bd367b 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/GelfLayout3Test.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/GelfLayout3Test.java
@@ -50,11 +50,13 @@ public class GelfLayout3Test {
         final JsonNode json = mapper.readTree(gelf);
         assertEquals("My Test Message", json.get("short_message").asText());
         assertEquals("myhost", json.get("host").asText());
-        assertNotNull(json.get("_loginId"));
-        assertEquals("rgoers", json.get("_loginId").asText());
-        assertNull(json.get("_internalId"));
-        assertNull(json.get("_requestId"));
+        assertNotNull(json.get("_mdc.loginId"));
+        assertEquals("rgoers", json.get("_mdc.loginId").asText());
+        assertNull(json.get("_mdc.internalId"));
+        assertNull(json.get("_mdc.requestId"));
         String message = json.get("full_message").asText();
+        assertTrue(message.contains("loginId=rgoers"));
+        assertTrue(message.contains("GelfLayout3Test"));
         assertNull(json.get("arg1"));
         assertNull(json.get("arg2"));
     }
@@ -76,17 +78,17 @@ public class GelfLayout3Test {
         assertEquals("arg1=\"test1\" arg3=\"test3\" message=\"My Test Message\"",
                 json.get("short_message").asText());
         assertEquals("myhost", json.get("host").asText());
-        assertNotNull(json.get("_loginId"));
-        assertEquals("rgoers", json.get("_loginId").asText());
-        assertNull(json.get("_internalId"));
-        assertNull(json.get("_requestId"));
+        assertNotNull(json.get("_mdc.loginId"));
+        assertEquals("rgoers", json.get("_mdc.loginId").asText());
+        assertNull(json.get("_mdc.internalId"));
+        assertNull(json.get("_mdc.requestId"));
         String msg = json.get("full_message").asText();
         assertTrue(msg.contains("loginId=rgoers"));
         assertTrue(msg.contains("GelfLayout3Test"));
         assertTrue(msg.contains("arg1=\"test1\""));
-        assertNull(json.get("arg2"));
-        assertEquals("test1", json.get("_arg1").asText());
-        assertEquals("test3", json.get("_arg3").asText());
+        assertNull(json.get("map.arg2"));
+        assertEquals("test1", json.get("_map.arg1").asText());
+        assertEquals("test3", json.get("_map.arg3").asText());
     }
 
 }
diff --git a/log4j-core/src/test/resources/GelfLayout3Test.xml b/log4j-core/src/test/resources/GelfLayout3Test.xml
index e985737..6851ae1 100644
--- a/log4j-core/src/test/resources/GelfLayout3Test.xml
+++ b/log4j-core/src/test/resources/GelfLayout3Test.xml
@@ -20,7 +20,7 @@
   <Appenders>
     <List name="list">
       <GelfLayout host="myhost" includeThreadContext="true" threadContextIncludes="requestId,loginId"
-                  mapMessageIncludes="arg1,arg2,arg3">
+                  mapMessageIncludes="arg1,arg2,arg3" threadContextPrefix="mdc." mapPrefix="map.">
         <MessagePattern>%d [%t] %-5p %X{requestId, loginId} %C{1.}.%M:%L - %m%n"</MessagePattern>
         <KeyValuePair key="foo" value="FOO"/>
         <KeyValuePair key="runtime" value="$${java:runtime}"/>
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index d6a03a3..ca116b0 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -169,6 +169,9 @@
       </action>
     </release>
     <release version="2.15.0" date="2021-MM-DD" description="GA Release 2.15.0">
+      <action issue="LOG4J2-3049" dev="rgoers" type="add">
+        Allow MapMessage and ThreadContext attributes to be prefixed.
+      </action>
       <action issue="LOG4J2=3048" dev="rgoers" type="add">
         Add improved MapMessge support to GelfLayout.
       </action>
diff --git a/src/site/asciidoc/manual/layouts.adoc b/src/site/asciidoc/manual/layouts.adoc
index 03dc6c5..6ae5df8 100644
--- a/src/site/asciidoc/manual/layouts.adoc
+++ b/src/site/asciidoc/manual/layouts.adoc
@@ -243,6 +243,10 @@ attribute only applies when includeMapMessage="true" is specified. If mapMessage
 are also specified this attribute will override them. MapMessage fields specified here that
 have no value will be omitted.
 
+|mapPrefix
+|String
+|A String to prepend to all elements of the MapMessage when rendered as a field. Defaults to an empty String.
+
 |messagePattern
 |String
 |The pattern to use to format the String. A messagePattern and patternSelector cannot both be
@@ -259,7 +263,6 @@ See link:#PatternSelectors[Pattern Selectors] for information on how to specify
 PatternSelector.
 See link:#PatternLayout[PatternLayout] for information on the pattern strings.
 
-
 |threadContextExcludes
 |String
 |A comma separated list of ThreadContext attributes to exclude when formatting the event. This
@@ -272,6 +275,10 @@ are also specified this attribute will be ignored.
 attribute only applies when includeThreadContext="true" is specified. If threadContextExcludes
 are also specified this attribute will override them. ThreadContext fields specified here that
 have no value will be omitted.
+
+|ThreadContextPrefix
+|String
+|A String to prepend to all elements of the ThreadContextMap when rendered as a field. Defaults to an empty String.
 |===
 
 To include any custom field in the output, use following syntax: