Children threads not picking up mdc value properly using logback pattern

I am having trouble with MDC and logback. I am reading that MDC is thread local and whatever is put into the MDC is picked up by children threads.
But this is not what’s happening. It is threadlocal but doesn’t look like children thread pick up the mdc values.

Here is my log:
17:40:47.362 http-bio-8090-exec-32 myapp.FoouserApp [DEBUG]

  • Application init method
    17:40:47.367 http-bio-8090-exec-32 myapp.FoouserApp [DEBUG]
  • Putting foouser into MDC in loadProtectedResourceInit()
    17:40:47.726 http-bio-8090-exec-32 myapp.util.LdapUtil [WARN]
    foouser - Could not find user! [ foouser ]

17:40:47.874 http-bio-8090-exec-32 myapp.components.AutoCompleteField [DEBUG]
foouser - TextSearchField()
17:40:47.875 http-bio-8090-exec-32 myapp.pages.PageHome [INFO]
foouser - Home [PageHome, Home, 0 ]

17:40:47.875 http-bio-8090-exec-32 myapp.pages.PageHome [INFO]
foouser - userAgent browser: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36
17:40:52.525 http-bio-8090-exec-32 myapp.pages.PageHome [INFO]
foouser - Home - End
17:40:52.542 http-bio-8090-exec-32 myapp.pages.PageHome [DEBUG]
foouser - bean last record - 862274
17:40:52.544 http-bio-8090-exec-32 myapp.content.MainContent [INFO]
foouser - Create of focus Home with ID PageHome
Jun 9, 2013 5:40:54 PM org.icepush.servlet.EnvironmentAdaptingServlet
INFO: Adapting to Servlet 3.0 AsyncContext environment
Jun 9, 2013 5:40:54 PM org.icepush.servlet.AsyncAdaptingServlet
INFO: Using Servlet 3.0 AsyncContext
Jun 9, 2013 5:40:55 PM org.icepush.servlet.EnvironmentAdaptingServlet service
INFO: Falling back to Thread Blocking environment
17:41:01.136 http-bio-8090-exec-44 myapp.WindowMainFrame [DEBUG]

  • FoouserHome-getButton.getCaption [ SCR ]
    was clicked
    17:41:01.144 http-bio-8090-exec-44 myapp.WindowMainFrame [DEBUG]
  • FoouserHome-getButton.getDebugId [ accordionMenu:d5bacfce-bf77-43bc-a715-a38d4bb71c7c ]

17:41:01.159 http-bio-8090-exec-44 myapp.pages.PageSCR [DEBUG]

  • User in mdc is null
    17:41:01.342 http-bio-8090-exec-44 myapp.content.MainContent [INFO]
  • Create of focus S with ID SCR

So the problem is I put the user into the MDC in the application class and the logback pattern picks that up just fine.
But once I click on a UI element which opens a new tab and starts a new thread, that new thread sort of loses the mdc value.

And its not very reliable either. Sometimes it works and sometimes it doesn’t.

I was just wondering the same thing.

Where do you set the MDC values? Each request can be handled in a different thread so you’ll have to set them for each request, normally in a RequestHandler implementation.