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

MDC.getCopyOfContextMap can return null, but MDC.setContextMap doesn't accept it

    XMLWordPrintable

Details

    • Icon: Improvement Improvement
    • Resolution: Unresolved
    • Icon: Minor Minor
    • 1.5.12
    • 1.0.13
    • None
    • None

    Description

      When using MDC and thread pools, code usually follows this pattern:

      final Map context = MDC.getCopyOfContextMap();
      executor.submit(new Runnable() {
      	@Override
      	public void run() {
      		MDC.setContextMap(context);
      	}
      

      But if no MDC variables are set, getCopyOfContextMap() returns null and setContextMap then blows up. LogbackMDCAdapter.setContextMap uses Map.putAll which doesn't accept null. Therefore, one has to wrap the call to setContextMap in an annoying null check.

      I suggest that setContextMap should handle a null argument gracefully by returning early. Alternatively, return Collections.emptyMap() or similar from getCopyOfContextMap.

      Attachments

        Activity

          People

            ceki Ceki Gülcü
            thomas11 Thomas Kappler
            Votes:
            3 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated: