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

Pascale, Peter H. Peter.Pascale at Pearson.com
Mon Feb 18 15:14:01 CET 2008

You could separate the log statements as a post-processing step
(generating the one-file-per-server-IP - say on a daily basis). We do
something like that, but in our case the aggregate log file has a value
by itself.


-----Original Message-----
From: logback-user-bounces at qos.ch [mailto:logback-user-bounces at qos.ch]
On Behalf Of Stefan Armbruster
Sent: Sunday, February 17, 2008 1:29 PM
To: logback users list
Subject: Re: [logback-user] using different appenders based on MDC value

Assume a clustered webapp distributed to multiple servers running tomcat
app1, app2, app3). Since appX are diskless servers, the webapp's
simply uses SocketAppender to redirect all log messagess to a central
The webapp uses a servlet filter (similar to 
http://logback.qos.ch/manual/mdc.html) to populate the MDC with the
IP number. 
The central logging server receives all log messages from app1-appX
SimpleSocketServer and writes them to a single file using the following 

<appender name="default" 

 <layout class="ch.qos.logback.classic.PatternLayout">
   <Pattern>%d{ISO8601} [%thread] S:%X{server} %-5level 
     %logger{36} - %msg%n</Pattern>

Note that the server's IP appears inside the logfile, nothing new until
The goal is to have multiple logfiles ( 1 per server IP number), e.g.

So the appender must be aware of the server's ip number stored in the
MDC map.
I hope this spreads some more light on my use case.


Am Sonntag, 17. Februar 2008 schrieb Ceki Gulcu:
> 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
> > 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
> > is supplied with context="A", it should be written to
> > 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
> > 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
> > 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
Logback-user mailing list
Logback-user at qos.ch

This email may contain material confidential to
Pearson.  If you were not an intended recipient, 
please notify the sender and delete all copies. 
We may monitor email to and from our network. 

More information about the Logback-user mailing list