[logback-user] Conditional Processing using MDC key/value pairs

ceki ceki at qos.ch
Thu May 24 12:09:19 CEST 2012


Hi Shane,

Apologies for my previous empty message sent by error. More below.

On 15.05.2012 15:01, Shane Kelly wrote:

[snip]

> Why do I want to do this? Well, in this instance I have a small web-app
> consisting of a number of servlets. The servlets are
> stateless/sessionless - they service individual requests from clients,
> but no session is maintained. In the absence of a unique session ID (I
> could force a session to be created using getSession() - but that would
> be wasteful of resources for servlets which don't really need one) I
> would like to insert another UUID into the log files generated by the
> server so that its easy to zone in on all the log messages generated as
> the result of a single incoming client request.

I've read your email with interest. From what I could understand, why 
not just have one pattern which has %X{MyApp-UUID}? If no MyApp-UUID 
value is available %X{MyApp-UUID} will just print empty.


[snip]

> So, to recap:
>
> (a) can conditional processing expressions access key/value pairs in the
> MDC map?

No. Putting "automatic re-configuration on change" aside, you configure 
logback only once at application initialization time. On the other hand, 
MDC values can be changed freely per thread.  Thus, it makes no sense 
(to me) to do conditional configuration based on  MDC values.

> From my basic experimentation, I'm guessing the answer is no -
> but then I could just be referencing it incorrectly
> (b) if the answer to (a) is no, then is there a more suitable approach I
> should be using instead.
>
> Note: Since conditional processing can be used anywhere within the
> logback configuration I may want to use a similar technique for
> completely different purposes in future, the example I've given just
> happens to focus on encoder patterns for illustrative purposes. So I'm
> after a generic solution that allows conditional processing based on MDC
> key/value pairs.

Have you looked at SiftingAppender [1]? It dynamically creates new 
appenders instances based on runtime criteria.

HTH,

[1] http://logback.qos.ch/manual/appenders.html#SiftingAppender

-- 
Ceki
http://twitter.com/#!/ceki


More information about the Logback-user mailing list