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.
privatestaticfinal 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;
}
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira