[logback-user] Modifing MDCPropertyMap of LoggingEvents
Cemo
cemalettin.koc at gmail.com
Mon Mar 9 21:03:47 CET 2015
I have a layout which is generating JSON from logging events and MDC
Context. MDC is great and I want to add parameters to MDC Context
conditionally. For example, I would like add request information as JSON
into MDC conditionally on Error Level log entries. I am not familiar with
Logback internals but gave a try with Logback Filter implementation. I have
used:
public FilterReply decide(ILoggingEvent event) {
// simplified for demo // conditionally I am putting some
attributes here Map<String, String> mdcPropertyMap =
event.getMDCPropertyMap();
if (event.getLevel().isGreaterOrEqual(Level.ERROR)) {
mdcPropertyMap.put("demo", "demo");
}
return FilterReply.ACCEPT;
}
I am expecting mdcPropertyMap to be cleared for each event but it does not.
LoggingEvent has a internal CACHED_NULL_MAP map and its modified per touch.
private static final Map<String, String> CACHED_NULL_MAP = new
HashMap<String, String>();
I could not be sure but it seems a little bid buggy to me. Can someone
verify this behaviour?
For those who are interested why I am not using MDC.put in a Servlet
Filter implementation because I am conditionally adding some
attributes. Serialising HttpRequest into JSON is not cheap task but
having Http Request information is giving great insights about error.
Using Filter is the only way I found to add conditionally attributes
so far. What I would like to use is:
public FilterReply decide(ILoggingEvent event) {
if (event.getLevel().isGreaterOrEqual(Level.WARN)) {
MDC.put("demo", "demo");
}
return FilterReply.ACCEPT;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/logback-user/attachments/20150309/cfa9f8a2/attachment.html>
More information about the Logback-user
mailing list