Repository: logging-log4j2
Updated Branches:
  refs/heads/master 8d3225972 -> d6cfb59c7


LOG4J2-1022 - allow a list of keys to be specified in the MdcPatternConverter


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/d6cfb59c
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/d6cfb59c
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/d6cfb59c

Branch: refs/heads/master
Commit: d6cfb59c715ed069f18240893e2714f47199cc88
Parents: 8d32259
Author: Ralph Goers <[email protected]>
Authored: Mon May 18 15:07:55 2015 -0700
Committer: Ralph Goers <[email protected]>
Committed: Mon May 18 15:07:55 2015 -0700

----------------------------------------------------------------------
 .../log4j/core/pattern/MdcPatternConverter.java | 28 +++++--
 .../core/pattern/MdcPatternConverterTest.java   | 86 ++++++++++++++++++++
 src/changes/changes.xml                         |  3 +
 src/site/xdoc/manual/layouts.xml.vm             | 19 +++--
 4 files changed, 122 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d6cfb59c/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/MdcPatternConverter.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/MdcPatternConverter.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/MdcPatternConverter.java
index 6ca2642..e33b89a 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/MdcPatternConverter.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/MdcPatternConverter.java
@@ -30,7 +30,7 @@ import org.apache.logging.log4j.core.config.plugins.Plugin;
  * within the property bundle
  * when this pattern converter has the option set.
  */
- @Plugin(name = "MdcPatternConverter", category = PatternConverter.CATEGORY)
+@Plugin(name = "MdcPatternConverter", category = PatternConverter.CATEGORY)
 @ConverterKeys({ "X", "mdc", "MDC" })
 public final class MdcPatternConverter extends LogEventPatternConverter {
     /**
@@ -74,7 +74,7 @@ public final class MdcPatternConverter extends 
LogEventPatternConverter {
                 return;
             }
             final StringBuilder sb = new StringBuilder("{");
-            final Set<String> keys = new TreeSet<>(contextMap.keySet());
+            final Set<String> keys = new TreeSet<String>(contextMap.keySet());
             for (final String key : keys) {
                 if (sb.length() > 1) {
                     sb.append(", ");
@@ -85,11 +85,27 @@ public final class MdcPatternConverter extends 
LogEventPatternConverter {
             sb.append('}');
             toAppendTo.append(sb);
         } else if (contextMap != null) {
-            // otherwise they just want a single key output
-            final Object val = contextMap.get(key);
+            if (key.indexOf(',') > 0) {
+                String[] keys = key.split(",");
+                final StringBuilder sb = new StringBuilder("{");
+                for (String key : keys) {
+                    key = key.trim();
+                    if (contextMap.containsKey(key)) {
+                        if (sb.length() > 1) {
+                            sb.append(", ");
+                        }
+                        sb.append(key).append('=').append(contextMap.get(key));
+                    }
+                }
+                sb.append('}');
+                toAppendTo.append(sb);
+            } else {
+                // otherwise they just want a single key output
+                final Object val = contextMap.get(key);
 
-            if (val != null) {
-                toAppendTo.append(val);
+                if (val != null) {
+                    toAppendTo.append(val);
+                }
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d6cfb59c/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MdcPatternConverterTest.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MdcPatternConverterTest.java
 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MdcPatternConverterTest.java
new file mode 100644
index 0000000..361da6d
--- /dev/null
+++ 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/MdcPatternConverterTest.java
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.pattern;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.ThreadContext;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.message.MapMessage;
+import org.apache.logging.log4j.message.Message;
+import org.apache.logging.log4j.message.SimpleMessage;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ *
+ */
+public class MdcPatternConverterTest {
+
+    @Test
+    public void testConverter() {
+
+        final Message msg = new SimpleMessage("Hello");
+        ThreadContext.put("subject", "I");
+        ThreadContext.put("verb", "love");
+        ThreadContext.put("object", "Log4j");
+        final MdcPatternConverter converter = 
MdcPatternConverter.newInstance(null);
+        final LogEvent event = new Log4jLogEvent("MyLogger", null, null, 
Level.DEBUG, msg, null);
+        final StringBuilder sb = new StringBuilder();
+        converter.format(event, sb);
+        final String str = sb.toString();
+        String expected = "{object=Log4j, subject=I, verb=love}";
+        assertTrue("Incorrect result. Expected " + expected + ", actual " + 
str, str.equals(expected));
+    }
+
+    @Test
+    public void testConverterWithKey() {
+
+        final Message msg = new SimpleMessage("Hello");
+        String [] options = new String[] {"object"};
+        ThreadContext.put("subject", "I");
+        ThreadContext.put("verb", "love");
+        ThreadContext.put("object", "Log4j");
+        final MdcPatternConverter converter = 
MdcPatternConverter.newInstance(options);
+        final LogEvent event = new Log4jLogEvent("MyLogger", null, null, 
Level.DEBUG, msg, null);
+        final StringBuilder sb = new StringBuilder();
+        converter.format(event, sb);
+        final String str = sb.toString();
+        final String expected = "Log4j";
+        assertEquals(expected, str);
+    }
+
+    @Test
+    public void testConverterWithKeys() {
+
+        final Message msg = new SimpleMessage("Hello");
+        String [] options = new String[] {"object, subject"};
+        ThreadContext.put("subject", "I");
+        ThreadContext.put("verb", "love");
+        ThreadContext.put("object", "Log4j");
+        final MdcPatternConverter converter = 
MdcPatternConverter.newInstance(options);
+        final LogEvent event = new Log4jLogEvent("MyLogger", null, null, 
Level.DEBUG, msg, null);
+        final StringBuilder sb = new StringBuilder();
+        converter.format(event, sb);
+        final String str = sb.toString();
+        final String expected = "{object=Log4j, subject=I}";
+        assertEquals(expected, str);
+    }
+}
+

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d6cfb59c/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 68a1914..a51769f 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -24,6 +24,9 @@
   </properties>
   <body>
     <release version="2.4" date="2015-MM-DD" description="GA Release 2.4">
+      <action issue="LOG4J2-1022" dev="rgoers" type="update">
+        Allow a list of keys to be specified in the MDC pattern converter.
+      </action>
       <action issue="LOG4J2-1019" dev="ggregory" type="fix">
         ZipCompressAction leaves files open until GC when an IO error takes 
place.
       </action>

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d6cfb59c/src/site/xdoc/manual/layouts.xml.vm
----------------------------------------------------------------------
diff --git a/src/site/xdoc/manual/layouts.xml.vm 
b/src/site/xdoc/manual/layouts.xml.vm
index cedf18e..e79b95a 100644
--- a/src/site/xdoc/manual/layouts.xml.vm
+++ b/src/site/xdoc/manual/layouts.xml.vm
@@ -1039,23 +1039,26 @@ WARN  [main]: Message 2</pre>
             </tr>
             <tr>
               <td align="center">
-                <b>X</b>{key}<br />
-                <b>mdc</b>{key}<br />
-                <b>MDC</b>{key}
+                <b>X</b>{key[,key2...]}<br />
+                <b>mdc</b>{key[,key2...]}<br />
+                <b>MDC</b>{key[,key2...]}
               </td>
               <td>
-                <p>Outputs the Thread Context Map (also known as the Mapped 
Diagnostic Context or MDC)
+                <>Outputs the Thread Context Map (also known as the Mapped 
Diagnostic Context or MDC)
                   associated with the thread that generated the logging event. 
The
                   <b>X</b>
-                  conversion character can be followed by the key for the
+                  conversion character can be followed by one or more keys for 
the
                   map placed between braces, as in
                   <b>%X{clientNumber}</b>
                   where
                   <code>clientNumber</code>
                   is the key. The value in the MDC
-                  corresponding to the key will be output. If no additional 
sub-option
-                  is specified, then the entire contents of the MDC key value 
pair set
-                  is output using a format {{key1,val1},{key2,val2}}
+                  corresponding to the key will be output.</p>
+                <p>If a list of keys are provided, such as <b>%X{name, 
number}</b>, then each key that is present in the
+                    ThreadContext will be output using the format {name=val1, 
number=val2}. The key/value pairs will be
+                  printed in the order they appear in the list.</p>
+                <p>If no sub-options are specified then the entire contents of 
the MDC key value pair set
+                  is output using a format {key1=val1, key2=val2}. The 
key/value pairs will be printed in sorted order.
                 </p>
                 <p>See the
                   <a class="javadoc" 
href="../log4j-api/apidocs/org/apache/logging/log4j/ThreadContext.html">ThreadContext</a>

Reply via email to