Uploaded image for project: 'logback'
  1. logback
  2. LOGBACK-1057

Modifing MDCPropertyMap of LoggingEvents

    XMLWordPrintable

Details

    • Icon: Bug Bug
    • Resolution: Not a bug
    • Icon: Major Major
    • None
    • None
    • None
    • None

    Description

      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;
         }
      

      Attachments

        Activity

          People

            ceki Ceki Gülcü
            cemo cemalettin koc
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: