[LOGBack-dev] svn commit: r563 - logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/pattern

noreply.seb at qos.ch noreply.seb at qos.ch
Tue Sep 12 16:07:48 CEST 2006


Author: seb
Date: Tue Sep 12 16:07:48 2006
New Revision: 563

Modified:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/pattern/MDCConverter.java

Log:
modified MDCConverter to return all the values present in the MDC when no key is specified

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/pattern/MDCConverter.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/pattern/MDCConverter.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/pattern/MDCConverter.java	Tue Sep 12 16:07:48 2006
@@ -1,5 +1,7 @@
 package ch.qos.logback.classic.pattern;
 
+import java.util.Collection;
+import java.util.Iterator;
 import java.util.Map;
 
 import ch.qos.logback.classic.spi.LoggingEvent;
@@ -7,7 +9,8 @@
 public class MDCConverter extends ClassicConverter {
 
   String key;
-  
+  private static final String EMPTY_STRING = "";
+
   public MDCConverter() {
   }
 
@@ -16,25 +19,41 @@
     key = getFirstOption();
     super.start();
   }
-  
+
   @Override
   public void stop() {
     key = null;
     super.stop();
   }
-  
+
   @Override
   public String convert(Object event) {
     LoggingEvent loggingEvent = (LoggingEvent) event;
     Map<String, String> mdcPropertyMap = loggingEvent.getMDCPropertyMap();
-    if (mdcPropertyMap != null) {
-      String value = loggingEvent.getMDCPropertyMap().get(key);
-      if (value != null) {
-        return value;
+
+    if (mdcPropertyMap == null) {
+      return EMPTY_STRING;
+    }
+
+    if (key == null) {
+      // if no key is specified, return all the
+      // values present in the MDC, separated with a single space.
+      StringBuffer buf = new StringBuffer();
+      Collection<String> values = mdcPropertyMap.values();
+      Iterator it = values.iterator();
+      String value;
+      while (it.hasNext()) {
+        value = (String)it.next();
+        buf.append(value).append(' ');
       }
-      return "";
+      return buf.toString();
+    }
+
+    String value = loggingEvent.getMDCPropertyMap().get(key);
+    if (value != null) {
+      return value;
     } else {
-      return "";
+      return EMPTY_STRING;
     }
   }
 }



More information about the logback-dev mailing list