You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by ca...@apache.org on 2008/10/10 00:51:32 UTC

svn commit: r703285 - in /logging/log4j/trunk: src/changes/ src/main/java/org/apache/log4j/helpers/ tests/input/ tests/src/java/org/apache/log4j/ tests/witness/

Author: carnold
Date: Thu Oct  9 15:51:31 2008
New Revision: 703285

URL: http://svn.apache.org/viewvc?rev=703285&view=rev
Log:
Bug 45982: Support %X layout specification to output all MDC key-value pairs

Added:
    logging/log4j/trunk/tests/input/patternLayout.mdc.1.properties
      - copied, changed from r702979, logging/log4j/companions/extras/trunk/src/test/resources/org/apache/log4j/patternLayout.mdc.1.properties
    logging/log4j/trunk/tests/witness/patternLayout.mdc.1
      - copied unchanged from r702979, logging/log4j/companions/extras/trunk/src/test/resources/org/apache/log4j/patternLayout.mdc.1
Modified:
    logging/log4j/trunk/src/changes/changes.xml
    logging/log4j/trunk/src/main/java/org/apache/log4j/helpers/PatternParser.java
    logging/log4j/trunk/tests/src/java/org/apache/log4j/PatternLayoutTestCase.java

Modified: logging/log4j/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/src/changes/changes.xml?rev=703285&r1=703284&r2=703285&view=diff
==============================================================================
--- logging/log4j/trunk/src/changes/changes.xml (original)
+++ logging/log4j/trunk/src/changes/changes.xml Thu Oct  9 15:51:31 2008
@@ -65,6 +65,7 @@
        <action action="fix" issue="45969">SMTPAppender does not force evaluation of message at request time</action>
        <action action="fix" issue="25355">Support SSL transport in SMTPAppender</action>
        <action action="fix" issue="45980">SMTPAppender should have a sendOnClose option</action>
+       <action action="fix" issue="45982">Support %X layout specification to output all MDC key-value pairs</action>
     </release>
 
   

Modified: logging/log4j/trunk/src/main/java/org/apache/log4j/helpers/PatternParser.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/src/main/java/org/apache/log4j/helpers/PatternParser.java?rev=703285&r1=703284&r2=703285&view=diff
==============================================================================
--- logging/log4j/trunk/src/main/java/org/apache/log4j/helpers/PatternParser.java (original)
+++ logging/log4j/trunk/src/main/java/org/apache/log4j/helpers/PatternParser.java Thu Oct  9 15:51:31 2008
@@ -25,6 +25,8 @@
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.Map;
+import java.util.Arrays;
 
 // Contributors:   Nelson Minar <(n...@monkey.org>
 //                 Igor E. Poteryaev <ja...@mail.ru>
@@ -460,11 +462,29 @@
 
     public
     String convert(LoggingEvent event) {
-      Object val = event.getMDC(key);
-      if(val == null) {
-	return null;
+      if (key == null) {
+          StringBuffer buf = new StringBuffer("{");
+          Map properties = event.getProperties();
+          if (properties.size() > 0) {
+            Object[] keys = properties.keySet().toArray();
+            Arrays.sort(keys);
+            for (int i = 0; i < keys.length; i++) {
+                buf.append('{');
+                buf.append(keys[i]);
+                buf.append(',');
+                buf.append(properties.get(keys[i]));
+                buf.append('}');
+            }
+          }
+          buf.append('}');
+          return buf.toString();
       } else {
-	return val.toString();
+        Object val = event.getMDC(key);
+        if(val == null) {
+	        return null;
+        } else {
+	        return val.toString();
+        }
       }
     }
   }

Copied: logging/log4j/trunk/tests/input/patternLayout.mdc.1.properties (from r702979, logging/log4j/companions/extras/trunk/src/test/resources/org/apache/log4j/patternLayout.mdc.1.properties)
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/tests/input/patternLayout.mdc.1.properties?p2=logging/log4j/trunk/tests/input/patternLayout.mdc.1.properties&p1=logging/log4j/companions/extras/trunk/src/test/resources/org/apache/log4j/patternLayout.mdc.1.properties&r1=702979&r2=703285&rev=703285&view=diff
==============================================================================
--- logging/log4j/companions/extras/trunk/src/test/resources/org/apache/log4j/patternLayout.mdc.1.properties (original)
+++ logging/log4j/trunk/tests/input/patternLayout.mdc.1.properties Thu Oct  9 15:51:31 2008
@@ -14,9 +14,9 @@
 # limitations under the License.
 log4j.rootCategory=DEBUG, testAppender
 log4j.appender.testAppender=org.apache.log4j.FileAppender
-log4j.appender.testAppender.File=temp
+log4j.appender.testAppender.File=output/temp
 log4j.appender.testAppender.Append=false
-log4j.appender.testAppender.layout=org.apache.log4j.EnhancedPatternLayout 
+log4j.appender.testAppender.layout=org.apache.log4j.PatternLayout 
 log4j.appender.testAppender.layout.ConversionPattern=%-5p - %m %X%n
 
 #  Prevent internal log4j DEBUG messages from polluting the output. 

Modified: logging/log4j/trunk/tests/src/java/org/apache/log4j/PatternLayoutTestCase.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/tests/src/java/org/apache/log4j/PatternLayoutTestCase.java?rev=703285&r1=703284&r2=703285&view=diff
==============================================================================
--- logging/log4j/trunk/tests/src/java/org/apache/log4j/PatternLayoutTestCase.java (original)
+++ logging/log4j/trunk/tests/src/java/org/apache/log4j/PatternLayoutTestCase.java Thu Oct  9 15:51:31 2008
@@ -18,25 +18,17 @@
 package org.apache.log4j;
 
 import junit.framework.TestCase;
-import junit.framework.TestSuite;
-import junit.framework.Test;
-
-import org.apache.log4j.Logger;
-import org.apache.log4j.Level;
-import org.apache.log4j.MDC;
-import org.apache.log4j.NDC;
-
-import org.apache.log4j.util.LineNumberFilter;
-import org.apache.log4j.util.Transformer;
-import org.apache.log4j.util.Filter;
-import org.apache.log4j.util.ControlFilter;
+import org.apache.log4j.util.AbsoluteDateAndTimeFilter;
+import org.apache.log4j.util.AbsoluteTimeFilter;
 import org.apache.log4j.util.Compare;
+import org.apache.log4j.util.ControlFilter;
+import org.apache.log4j.util.Filter;
 import org.apache.log4j.util.ISO8601Filter;
-import org.apache.log4j.util.AbsoluteTimeFilter;
+import org.apache.log4j.util.JunitTestRunnerFilter;
+import org.apache.log4j.util.LineNumberFilter;
 import org.apache.log4j.util.RelativeTimeFilter;
-import org.apache.log4j.util.AbsoluteDateAndTimeFilter;
 import org.apache.log4j.util.SunReflectFilter;
-import org.apache.log4j.util.JunitTestRunnerFilter;
+import org.apache.log4j.util.Transformer;
 
 public class PatternLayoutTestCase extends TestCase {
 
@@ -285,6 +277,18 @@
     assertTrue(Compare.compare(FILTERED, "witness/patternLayout.14"));
   }
 
+    public void testMDC1() throws Exception {
+      PropertyConfigurator.configure("input/patternLayout.mdc.1.properties");
+      MDC.put("key1", "va11");
+      MDC.put("key2", "va12");
+      logger.debug("Hello World");
+      MDC.remove("key1");
+      MDC.remove("key2");
+
+      assertTrue(Compare.compare(TEMP, "witness/patternLayout.mdc.1"));
+    }
+
+
 
   void common() {
     String oldThreadName = Thread.currentThread().getName();
@@ -322,23 +326,4 @@
   }
 
 
-  public static Test suite() {
-    TestSuite suite = new TestSuite();
-    suite.addTest(new PatternLayoutTestCase("test1"));
-    suite.addTest(new PatternLayoutTestCase("test2"));
-    suite.addTest(new PatternLayoutTestCase("test3"));
-    suite.addTest(new PatternLayoutTestCase("test4"));
-    suite.addTest(new PatternLayoutTestCase("test5"));
-    suite.addTest(new PatternLayoutTestCase("test6"));
-    suite.addTest(new PatternLayoutTestCase("test7"));
-    suite.addTest(new PatternLayoutTestCase("test8"));
-    suite.addTest(new PatternLayoutTestCase("test9"));
-    suite.addTest(new PatternLayoutTestCase("test10"));
-    suite.addTest(new PatternLayoutTestCase("test11"));
-    suite.addTest(new PatternLayoutTestCase("test12"));
-    suite.addTest(new PatternLayoutTestCase("test13"));
-    suite.addTest(new PatternLayoutTestCase("test14"));
-    return suite;
-  }
-
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org