[logback-user] Question about MDC in servlet init() method

Ceki Gulcu listid at qos.ch
Sun Mar 2 20:53:33 CET 2008


Hello Stephan,

Values placed in the MDC are valid for the current thread only. When a servlet 
responds to an HTTP request, it may or may not be running under the same thread 
that invoked its init() method.

You have several options. You can set MDC at the beginning of your servlet's 
service method. If you have multiple servlets, you can set the MDC from a 
servlet *filter* for a group of servlets before they get invoked.

As for setting the applicationName for your web-application, I think this is 
such as general requirement that logback should support this feature out of the 
box. I am creating a bug report to remind myself.

I hope this helps,

Stefan Frings wrote:
> Hello,
> 
> Lets assume I call MDC.put("applicationName","whatever") in the
> servlet init() method. Can I read this later in the service()
> method?
> 
> I'm asking because some weeks ago I noticed that this did not work.
> I assumed that the threads running service() are not childrens of
> the thread that runs init().
> 
> However, I tried this again today, and this time it worked fine. I
> was not able to reproduce the problem.
> 
> But before I do such things now in real web applications, I like to
> be sure if thats Ok.
> 
> Can I set MDC variables in init() and read them in service() safely,
> or does this work only randomly or under special conditions?

-- 
Ceki Gülcü
QOS.ch is looking to hire talented developers in Switzerland.  If
interested, please contact c e k i @ q o s . c h




More information about the Logback-user mailing list