[logback-user] Logging "Cross Functional" categories

Ceki Gülcü listid at qos.ch
Thu Oct 26 00:04:48 CEST 2006


In a recent post log4j-user list a developer asked whether it was
possible for log4j to log differently depending on technical
partitions such as web-app as opposed to EJB.

The original discussion can be found at:

  http://marc.theaimsgroup.com/?t=116179536800005&r=1&w=2

A kind soul, namely James Stauffer, suggested filtering based on
MDC.

Here is an example of how you would do it in logback.

------------------------------------------
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("key", "val");
     logger.debug("I know me " + 1);

     StatusPrinter.print(lc);
   }
------------------------------------------
Here the associated config file, "mdcFilter.xml".

<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>
       <OnMatch>DENY</OnMatch>
       <Evaluator class="ch.qos.logback.classic.boolex.JaninoEventEvaluator">
         <Name>mdcEvaluator</Name>
         <Expression>mdc!=null &amp;&amp; 
"val".equals(mdc.get("key"))</Expression>
       </Evaluator>

     </filter>
   </appender>


   <root>
     <level value="debug" />
     <appender-ref ref="STDOUT" />
   </root>
</configuration>


The above has been tested on logback version 0.5.

It will yield the following output.

0    [main] DEBUG  - I know me 0
|-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - 
Ignoring debug attribute.
|-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to 
instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
|-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender 
as [STDOUT]
|-INFO in ch.qos.logback.core.joran.action.NestedComponentIA - is dmmed 
applicable for /configuration/appender/filter
|-INFO in ch.qos.logback.core.joran.action.NestedComponentIA - Pushing 
component <filter> on top of the object stack.
|-INFO in ch.qos.logback.core.joran.action.NestedComponentIA - is dmmed 
applicable for /configuration/appender/filter/Evaluator
|-INFO in ch.qos.logback.core.joran.action.NestedComponentIA - Pushing 
component <Evaluator> on top of the object stack.
|-INFO in ch.qos.logback.core.joran.action.AppenderAction - Popping 
appender named [STDOUT] from the object stack
|-INFO in ch.qos.logback.classic.joran.action.LevelAction - root level set 
to DEBUG
|-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching 
appender named [STDOUT to Logger[root]


Note that the last log statement is suppressed, which is the whole
point of the exercise.




-- 
Ceki Gülcü
Logback: The reliable, generic, fast and flexible logging framework for Java.
http://logback.qos.ch




More information about the Logback-user mailing list