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

ceki ceki at qos.ch
Thu May 24 11:52:46 CEST 2012

On 15.05.2012 15:01, Shane Kelly wrote:
> Folks,
> Just wondering if it is possible to perform conditional processing in
> Logback by inspecting the MDC for the presence of and/or value of
> certain keys. The documentation states "The condition is a Java
> expression in which only context properties or system properties are
> accessible" Which "context" is it referring to here? Is the MDC map
> visible to the conditional expression?
> 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.
> The approach I've taken here is to create a Servlet Filter which
> intercepts all URL request patterns (/*), generates a new UUID and
> inserts it into the MDC with a specific key (e.g. MyApp-UUID) and
> removes the key again after control is passed back from the servlet to
> the filter. I then updated the encoder pattern configuration for the
> appropriate appenders to insert %X{MyApp-UUID} - and this works as
> expected - I get a unique UUID value for all messages associated with
> the same servlet request. However, to take this one step further I would
> like to use different encoder patterns dependent on whether or not a
> specific MDC key is present.
> The following is some pseudo-config which hopefully illustrates the concept:
> ---------------------------
> <configuration>
> <appender name="MyAppender" class="...">
> <encoder>
> <if condition='isDefined("MyApp-UUID")'>
> <then><pattern>... [%X{AssetTracker-UUID}] ...</pattern></then>
> <else><pattern>... some other pattern ...</pattern></else>
> </if>
> </encoder>
> </appender>
> </configuration>
> ---------------------------
> So, to recap:
> (a) can conditional processing expressions access key/value pairs in the
> MDC map? 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.
> Regards,
> Shane
> _______________________________________________
> Logback-user mailing list
> Logback-user at qos.ch
> http://mailman.qos.ch/mailman/listinfo/logback-user


More information about the Logback-user mailing list