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

Ceki Gulcu listid at qos.ch
Sun Feb 17 19:54:31 CET 2008


Stefan,

Could you please expand on the use case?

Stefan Armbruster wrote:
> 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
> _______________________________________________
> Logback-user mailing list
> Logback-user at qos.ch
> http://qos.ch/mailman/listinfo/logback-user

-- 
-- Ceki Gülcü
QOS.ch is looking to hire talented developers in Switzerland.  If
interested, please contact c e k i @ q o s . c h




More information about the Logback-user mailing list