You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by Michel Rondeau <mi...@yahoo.fr> on 2004/06/10 21:46:07 UTC
Contribution - Generic MDC filter
I was surprise not to see in log4j 1.2.8 a generic filter on MDC
content. So here it is:
---------------------------------
/*
* Created on 2004-06-09
* Author: Michel Rondeau
*/
package org.apache.log4j.spi;
import org.apache.log4j.spi.Filter;
import org.apache.log4j.spi.LoggingEvent;
/**
* @author rondmic
*
* only keep request wich values were passed by param tag ID in
config file.
* Multiple values may be passed separating them with whatever
character not used in values.
*/
public class MDCGenericFilter extends Filter {
String values = null;
String MDC = null;
/* (non-Javadoc)
* @see
org.apache.log4j.spi.Filter#decide(org.apache.log4j.spi.LoggingEvent)
*/
public int decide(LoggingEvent event) {
int result=NEUTRAL;
// obtain the field to filter passed through Logger
String eventValue = (String)event.getMDC(MDC);
// is value in filtered values
if((values != null) && (eventValue != null) && (values.indexOf(
eventValue)>=0))
result = DENY;
return result;
}
/**
* @return Returns the values filtered.
*/
public String getValues() {
return values;
}
/**
* @param values The values to filtered
*/
public void setValues(String values) {
this.values = values;
}
/**
* @return Returns the mDC key.
*/
public String getMDC() {
return MDC;
}
/**
* @param mdc The mDC key to set.
*/
public void setMDC(String mdc) {
MDC = mdc;
}
}
---------------------------------
It is then easily configurable in log4j.xml:
---------------------------------
...
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<!--param name="threshold" value="ALL"/-->
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%4.4r %d %-5.5p
[%-20.20t][%-50.50c] stack:[%100.100x] RqstID:[%-10.10X{requestID}]
AppID:[%-3.3X{applicationID}] src:[%-12.12X{source}]
RqstID:[%-20.20X{user}] [%-200.200m]%n"/>
</layout>
<filter class="org.apache.log4j.spi.MDCGenericFilter">
<param name="values" value="00000,00051"/>
<param name="MDC" value="requestID"/>
</filter>
<filter class="org.apache.log4j.spi.MDCGenericFilter">
<param name="values" value="333;444;555"/>
<param name="MDC" value="applicationID"/>
</filter>
</appender>
...
---------------------------------
Hope it will help.
Michel Rondeau (from Québec, Canada)
Groupe CGI inc.
http://www.cgi.com
=====
__________________________________________________________
Veuillez excuser la publicité ci-dessous... c'est le prix de la gratuité !
__________________________________________________________
Lèche-vitrine ou lèche-écran ?
magasinage.yahoo.ca
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org