[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.

Peter


-----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
(e.g. 
app1, app2, app3). Since appX are diskless servers, the webapp's
logback.xml 
simply uses SocketAppender to redirect all log messagess to a central
logging 
server.
The webapp uses a servlet filter (similar to 
http://logback.qos.ch/manual/mdc.html) to populate the MDC with the
server's 
IP number. 
The central logging server receives all log messages from app1-appX
using 
SimpleSocketServer and writes them to a single file using the following 
config:

<appender name="default" 
class="ch.qos.logback.core.rolling.RollingFileAppender">
 <file>default.log</file>
 <rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
   <FileNamePattern>default-%d{yyyy-MM-dd}.log</FileNamePattern>
 </rollingPolicy>

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

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

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.

Regards,
Stefan


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

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