[logback-user] using different appenders based on MDC value

Stefan Armbruster ml001 at armbruster-it.de
Sun Feb 17 19:39:47 CET 2008


Hi,

is there a way to redirect log messages depending on a MDC variable to 
different appenders? The logback manual only uses MDC values for the pattern 
inside a single appender. 

Example: assume, the code sets a MDC variable "context". If a log message is 
supplied with context="A", it should be written to application_A.log, if it 
has context="B", the message should be written to application_B.log, and so 
on.

If this is not possible by default, it should be possible to write a kind of 
MDCAwareAppenderWrapper like this (only pseudo code below): 

public class MDCAwareAppenderWrapper<E> implements Appender<E> {
	Map<String,Appender> appenderMap;
	String mdcName;
	
	... setters and getters omitted

	public void doAppend(E event) {
		LoggingEvent le = (LoggingEvent)event;
		String mdcValue = le.getMDCPropertyMap().get(mdcName);
		Appender appender = appenderMap.get(mdcValue);
		appender.doAppend(event);
	}
}

MDCAwareAppenderWrapper is configured by a Map<String,Appender> that maps MDC 
values (A and B from the example above) to the real appenders. 

Any hints or comments on that?

Kind regards,
Stefan



More information about the Logback-user mailing list