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 bu...@apache.org on 2004/06/15 15:58:08 UTC

DO NOT REPLY [Bug 29574] New: - Contribution: Simple Generic MDC filter

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=29574>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=29574

Contribution: Simple Generic MDC filter

           Summary: Contribution: Simple Generic MDC filter
           Product: Log4j
           Version: 1.2beta
          Platform: Other
        OS/Version: Windows NT/2K
            Status: NEW
          Severity: Enhancement
          Priority: Other
         Component: Other
        AssignedTo: log4j-dev@jakarta.apache.org
        ReportedBy: michel.rondeau@bigfoot.com


This filter allows a simple filter on MDC elements. It provide a simpler syntax
than ExpressionFilter for those who only want to log based on a MDC value. 

Here is the code and a XML configuration sample:

---------------------------------
/*
 * 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>

...
---------------------------------

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org