[logback-user] MDC and env-entry

Ceki Gulcu ceki at qos.ch
Thu Feb 24 08:08:36 CET 2011


You could easyly write your own discriminator class combining 
JNDIBasedContextDiscriminator and MDCBasedDiscriminator. Here is an example:

public class CombinedDiscriminator extends ContextAwareBase implements
     Discriminator<ILoggingEvent> {

  private String defaultMDCVal = "default";

   public String getDiscriminatingValue(ILoggingEvent event) {
     String mdcValue = defaultMDCVal;
     Map<String, String> mdcMap = event.getMdc();
     if (mdcMap != null) {
       mdcValue = mdcMap.get(key);
     }
     String contextName =  event.getLoggerContextVO().getName();
     return contextName + mdcValue;
   }
   ..etc
}


HTH,
--
Ceki
On 23.02.2011 13:39, José Juan Montiel wrote:
> Hi,
>
>     Hi, again with another question.
>
>     With aspectj, in injecting a var in MCD, something like this...
>
>     MDC.put("code",var);
>
>     in some places, depending something, and i'm using this to generate a
>     diferent file for this something like
>
>     <appender name="APPENDER"
>     class="ch.qos.logback.classic.sift.SiftingAppender">
>     <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
>     <evaluator
>     class="ch.qos.logback.classic.boolex.JaninoEventEvaluator">
>     <expression>
>                         (marker != null)
>     &amp;&amp; (
>                             (marker.contains("ASPECTJ")) ||
>                             (marker.contains("SOAP")))
>     </expression>
>     </evaluator>
>     <onMatch>DENY</onMatch>
>     </filter>
>     <discriminator>
>     <Key>code</Key>
>     <DefaultValue>0000</DefaultValue>
>     </discriminator>
>     <sift>
>     <appender name="FILE-${code}"
>     class="ch.qos.logback.core.rolling.RollingFileAppender">
>     <prudent>${prudent}</prudent>
>
>     <!-- 5 l?neas de stacktrace para las excepciones -->
>     <encoder
>     class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
>     <Pattern>%date - [%thread] - %.-20M - %L -
>     cn=%contextName - [%thread] - [%X{code}] - %message%n
>     %exception{5}</Pattern>
>     </encoder>
>
>     <rollingPolicy
>     class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
>
>     <FileNamePattern>${USER_HOME}/${backupDir}${code}.%d${patronRolling}</FileNamePattern>
>
>     <MaxHistory>${maxHistory}</MaxHistory>
>     </rollingPolicy>
>     </appender>
>     </sift>
>     </appender>
>
>
>     ASPECTJ and SOAP are Markers of other traces, that i don't want to
>     see here.
>     This way i get
>
>     in my root log directory ${USER_HOME}/${backupDir} files like
>
>     0001.log
>     0002.log
>
>     But, now i have multile apps (wars and ejbs) where i use
>
>     web.xml
>
>     <?xml version="1.0" encoding="UTF-8"?>
>     <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="
>     http://java.sun.com/xml/ns/javaee" xmlns:web="
>     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="
>     http://java.sun.com/xml/ns/javaee
>     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID"
>     version="2.5">
>         ...
>
>     <env-entry>
>     <description>JNDI logging context for this app</description>
>     <env-entry-name>logback/context-name</env-entry-name>
>     <env-entry-type>java.lang.String</env-entry-type>
>     <env-entry-value>APP1</env-entry-value>
>     </env-entry>
>     <env-entry>
>     <description>URL for configuring logback context</description>
>     <env-entry-name>logback/configuration-resource</env-entry-name>
>     <env-entry-type>java.lang.String</env-entry-type>
>     <env-entry-value>logback.xml</env-entry-value>
>     </env-entry>
>
>         ...
>
>     </web-app>
>
>
>
>     ejb-jar.xml
>
>     <?xml version="1.0" encoding="UTF-8"?>
>     <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise
>     JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
>     <ejb-jar>
>     <enterprise-beans>
>     <session>
>                 ...
>
>     <env-entry>
>     <description>JNDI logging context for this app</description>
>     <env-entry-name>logback/context-name</env-entry-name>
>     <env-entry-type>java.lang.String</env-entry-type>
>     <env-entry-value>EJB1</env-entry-value>
>     </env-entry>
>     <env-entry>
>     <description>URL for configuring logback
>     context</description>
>
>     <env-entry-name>logback/configuration-resource</env-entry-name>
>     <env-entry-type>java.lang.String</env-entry-type>
>     <env-entry-value>logback.xml</env-entry-value>
>     </env-entry>
>
>                 ...
>     </session>
>     </enterprise-beans>
>
>         ...
>     </ejb-jar>
>
>     a env-entry to identificate. I know that insede file, i use
>     cn=%contextName
>     to see this context but...
>
>     How could i use this context like i use ${code} to separete this files
>     (0001.log, 0002.log) in diferents directories.
>
>
> I'll make more simple... how to mix
>
> <discriminator
> class="ch.qos.logback.classic.sift.JNDIBasedContextDiscriminator">
> <defaultValue>unknown</defaultValue>
> </discriminator>
>
> and
>
> <discriminator>
> <Key>code</Key>
> <DefaultValue>0000</DefaultValue>
> </discriminator>
>
> this is a MDC, form use %code (inside log line) like ${contextName} in
> name file.
>
> Can i have 2 discriminator inside 1 appender?
>
> PD: logback version 0.9.21.
>
> Thanks.
>
> --
> Jose Juan Montiel Martinez
> Eurobits Technologies
> Calle Musgo 3, 1ª Planta
> 28023 Madrid
> T +34-917080300
> F +34-913077480
> josejuan.montiel at eurobits.es <mailto:josejuan.montiel at eurobits.es>
> http://www.eurobits.com
>
>
>
> _______________________________________________
> Logback-user mailing list
> Logback-user at qos.ch
> http://qos.ch/mailman/listinfo/logback-user




More information about the Logback-user mailing list