[logback-dev] svn commit: r1250 - in logback/trunk/logback-classic/src: main/java/ch/qos/logback/classic main/java/ch/qos/logback/classic/pattern test/java/ch/qos/logback/classic/pattern

noreply.seb at qos.ch noreply.seb at qos.ch
Thu Jan 18 17:00:43 CET 2007


Author: seb
Date: Thu Jan 18 17:00:43 2007
New Revision: 1250

Added:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/pattern/MarkerConverter.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/pattern/MarkerConverterTest.java
Modified:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/PatternLayout.java

Log:
Added and tested new MarkerConverter class

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/PatternLayout.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/PatternLayout.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/PatternLayout.java	Thu Jan 18 17:00:43 2007
@@ -21,6 +21,7 @@
 import ch.qos.logback.classic.pattern.LineSeparatorConverter;
 import ch.qos.logback.classic.pattern.LoggerConverter;
 import ch.qos.logback.classic.pattern.MDCConverter;
+import ch.qos.logback.classic.pattern.MarkerConverter;
 import ch.qos.logback.classic.pattern.MessageConverter;
 import ch.qos.logback.classic.pattern.MethodOfCallerConverter;
 import ch.qos.logback.classic.pattern.NopThrowableInformationConverter;
@@ -100,6 +101,8 @@
         NopThrowableInformationConverter.class.getName());
 
     defaultConverterMap.put("caller", CallerDataConverter.class.getName());
+    
+    defaultConverterMap.put("marker", MarkerConverter.class.getName());
 
     defaultConverterMap.put("n", LineSeparatorConverter.class.getName());
   }

Added: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/pattern/MarkerConverter.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/pattern/MarkerConverter.java	Thu Jan 18 17:00:43 2007
@@ -0,0 +1,47 @@
+package ch.qos.logback.classic.pattern;
+
+import java.util.Iterator;
+
+import org.slf4j.Marker;
+
+import ch.qos.logback.classic.spi.LoggingEvent;
+
+/**
+ * Return the event's marker value(s).
+ * 
+ * @author Sébastien Pennec
+ */
+public class MarkerConverter extends ClassicConverter {
+  
+  private static String EMPTY = "";
+  private static String OPEN = "[ ";
+  private static String CLOSE = " ]";
+  private static String SEP = ", ";
+  
+  public String convert(LoggingEvent le) {
+    Marker marker = le.getMarker();
+    if (marker == null) {
+      return EMPTY;
+    }
+      
+    if (!marker.hasChildren()) {
+      return marker.getName();
+    }
+    
+    Iterator it = marker.iterator();
+    Marker child;
+    StringBuffer sb = new StringBuffer(marker.getName());
+    sb.append(' ').append(OPEN);
+    while(it.hasNext()) {
+      child = (Marker)it.next();
+      sb.append(child.getName());
+      if (it.hasNext()) {
+        sb.append(SEP);
+      }
+    }
+    sb.append(CLOSE);
+      
+    return sb.toString();  
+  }
+
+}

Added: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/pattern/MarkerConverterTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/pattern/MarkerConverterTest.java	Thu Jan 18 17:00:43 2007
@@ -0,0 +1,69 @@
+package ch.qos.logback.classic.pattern;
+
+import junit.framework.TestCase;
+
+import org.slf4j.Marker;
+import org.slf4j.MarkerFactory;
+
+import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.spi.LoggingEvent;
+
+public class MarkerConverterTest extends TestCase {
+  
+  LoggerContext lc;
+  Marker marker;
+  MarkerConverter converter;
+  
+  public void setUp() throws Exception {
+    lc = new LoggerContext();
+    converter = new MarkerConverter();
+    converter.start();
+  }
+  
+  public void tearDown() throws Exception {
+    lc = null;
+    converter.stop();
+    converter = null;
+  }
+
+  public void testWithNullMarker() {
+    marker = null;
+    String result = converter.convert(createLoggingEvent());
+    assertEquals("", result);
+  }
+  
+  public void testWithMarker() {
+    String name = "test";
+    marker = MarkerFactory.getMarker(name);
+    String result = converter.convert(createLoggingEvent());
+    assertEquals(name, result);
+  }
+  
+  public void testWithOneChildMarker() {
+    marker = MarkerFactory.getMarker("test");
+    marker.add(MarkerFactory.getMarker("child"));
+    
+    String result = converter.convert(createLoggingEvent());
+    
+    assertEquals("test [ child ]", result);
+  }
+  
+  public void testWithSeveralChildMarker() {
+    marker = MarkerFactory.getMarker("testParent");
+    marker.add(MarkerFactory.getMarker("child1"));
+    marker.add(MarkerFactory.getMarker("child2"));
+    marker.add(MarkerFactory.getMarker("child3"));
+    
+    String result = converter.convert(createLoggingEvent());
+    
+    assertEquals("testParent [ child1, child2, child3 ]", result);
+  }
+  
+  private LoggingEvent createLoggingEvent() {
+    LoggingEvent le = new LoggingEvent(this.getClass().getName(), lc.getLogger(LoggerContext.ROOT_NAME),
+        Level.DEBUG, "test message", null, null);
+    le.setMarker(marker);
+    return le;
+  }
+}



More information about the logback-dev mailing list