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

MDC content is not copied to child threads

    XMLWordPrintable

Details

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Critical Critical
    • None
    • None
    • logback-classic
    • None
    • Operating System: All
      Platform: All

    • 133

    Description

      I havn't found neither at slf4j nor logback pages any information if a child thread inherits the MDC content of its parent thread when it is created.

      The log4j Javadoc of the MDC class states: "The MDC is managed on a per thread basis. A child thread automatically inherits a copy of the mapped diagnostic context of its parent."

      But with logback 0.9.8 the MDC is always empty for a new thread. Is there a reason for that?

      Within my application it only make sense if the child thread MDC is initially filled with content of the parent thread. Also for users of the log4j MDC this might be a critical issue preventing them from switching to logback.

      The LogbackMDCAdapter can easily be changed to achieve the inheriting feature:

      public class LogbackMDCAdapter implements MDCAdapter {

      private final InheritableThreadLocal<HashMap<String, String>> threadLocal = new InheritableThreadLocal<HashMap<String, String>>()
      {

      @Override
      @SuppressWarnings("unchecked")
      protected HashMap<String, String> childValue(HashMap<String, String> parentValue)

      { return (parentValue!=null) ? (HashMap<String, String>)parentValue.clone() : null; }

      };
      ...
      }

      Attachments

        Activity

          People

            logback-dev Logback dev list
            mfranz@intershop.de Michael Franz
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: