[logback-dev] svn commit: r780 - in logback/trunk/logback-classic: . examples/src/chapter5 src/main/java/ch/qos/logback/classic/boolex

noreply.ceki at qos.ch noreply.ceki at qos.ch
Wed Oct 25 23:35:04 CEST 2006


Author: ceki
Date: Wed Oct 25 23:35:03 2006
New Revision: 780

Added:
   logback/trunk/logback-classic/examples/src/chapter5/GoMDC.java
   logback/trunk/logback-classic/mdcfilter.xml
Modified:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/boolex/JaninoEventEvaluator.java

Log:

Added MDC support to JaninoEventEvaluator, with moderate success

Added: logback/trunk/logback-classic/examples/src/chapter5/GoMDC.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/examples/src/chapter5/GoMDC.java	Wed Oct 25 23:35:03 2006
@@ -0,0 +1,34 @@
+package chapter5;
+
+import org.slf4j.LoggerFactory;
+
+import ch.qos.logback.classic.Logger;
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.MDC;
+import ch.qos.logback.classic.joran.JoranConfigurator;
+import ch.qos.logback.core.joran.spi.JoranException;
+import ch.qos.logback.core.util.StatusPrinter;
+
+public class GoMDC {
+
+  public static void main(String[] args)  {
+    Logger logger = (Logger) LoggerFactory
+        .getLogger(GoMDC.class);
+    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
+
+    try {
+      JoranConfigurator configurator = new JoranConfigurator();
+      configurator.setContext(lc);
+      configurator.doConfigure("mdcFilter.xml");
+      
+    } catch (JoranException je) {
+      StatusPrinter.print(lc);
+    }
+
+    logger.debug("I know me " + 0);
+    MDC.put("k", "v");
+    logger.debug("I know me " + 1);
+    
+    StatusPrinter.print(lc);
+  }
+}
\ No newline at end of file

Added: logback/trunk/logback-classic/mdcfilter.xml
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/mdcfilter.xml	Wed Oct 25 23:35:03 2006
@@ -0,0 +1,28 @@
+<configuration>
+
+  <appender name="STDOUT"
+    class="ch.qos.logback.core.ConsoleAppender">
+    <layout class="ch.qos.logback.classic.PatternLayout">
+      <param name="Pattern"
+        value="%-4relative [%thread] %-5level %X{testKey} - %msg%n" />
+    </layout>
+    <filter class="ch.qos.logback.core.filter.EvaluatorFilter">  
+      <Name>myFilter</Name>
+      <x>DENY</x>
+      <Evaluator class="ch.qos.logback.classic.boolex.JaninoEventEvaluator">
+        <Name>mdcEvaluator</Name>
+        <Expression>mdc!=null &amp;&amp; mdc.get("key").equals("val")</Expression>
+      </Evaluator>
+     
+    </filter>
+  </appender>
+
+<!-- <Expression>"v".equals(mdc.get("k"))</Expression>
+-->
+
+  <root>
+    <level value="debug" />
+    <appender-ref ref="STDOUT" />
+  </root>
+</configuration>
+

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/boolex/JaninoEventEvaluator.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/boolex/JaninoEventEvaluator.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/boolex/JaninoEventEvaluator.java	Wed Oct 25 23:35:03 2006
@@ -2,6 +2,7 @@
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 
 import org.slf4j.Marker;
 
@@ -34,6 +35,7 @@
     DEFAULT_PARAM_NAME_LIST.add("level");
     DEFAULT_PARAM_NAME_LIST.add("timeStamp");
     DEFAULT_PARAM_NAME_LIST.add("marker");
+    DEFAULT_PARAM_NAME_LIST.add("mdc");
     DEFAULT_PARAM_NAME_LIST.add("throwable");
 
     
@@ -48,9 +50,14 @@
     DEFAULT_PARAM_TYPE_LIST.add(int.class);
     DEFAULT_PARAM_TYPE_LIST.add(long.class);
     DEFAULT_PARAM_TYPE_LIST.add(Marker.class);
+    DEFAULT_PARAM_TYPE_LIST.add(Map.class);
     DEFAULT_PARAM_TYPE_LIST.add(Throwable.class);
   }
   
+  
+  public JaninoEventEvaluator() {
+    
+  }
   protected String getDecoratedExpression() {
     return IMPORT_LEVEL + getExpression();
   }
@@ -94,6 +101,7 @@
     values[i++] = loggingEvent.getLevel().toInteger();
     values[i++] = new Long(loggingEvent.getTimeStamp());
     values[i++] = loggingEvent.getMarker();
+    values[i++] = loggingEvent.getMDCPropertyMap();
     if (loggingEvent.getThrowableInformation() != null) {
       values[i++] = loggingEvent.getThrowableInformation().getThrowable();
     } else {



More information about the logback-dev mailing list