As I see it, the problem is that at least some of the users seem to be 
expecting Log4j to magically know when to call clear. It can't. The application 
has to do it.

All the MDC is is a ThreadLocal where each Thread has its own Map associated 
with the ThreadLocal. You can get and put all you want. In a webapp, of course, 
if you add stuff to the MDC at the beginning of a request then you need to 
remove it all at the end of the request.  The normal pattern is

MDC.put(key, value);
try {
   call servlet
} finally {
  MDC.remove(key);
}

The problem with this is that it still leaves an empty Map in the ThreadLocal. 
So when remove() removes the last element from the Map then the Map is removed 
from the ThreadLocal.  

FWIW, Logback suffered a similar issue in 
http://jira.qos.ch/browse/LBCLASSIC-183. 

Ralph


On May 29, 2012, at 10:31 PM, Christian Grobmeier wrote:

> Bringing this to the mailinglist.
> 
> Ralph, I understand what you explained below, but I am lost on how to
> fix it. Do you have any more suggestions?
> I must actually admit that I am not familiar with MDC at all, probably
> that's my problem with it.
> 
> On Wed, May 30, 2012 at 7:26 AM,  <[email protected]> wrote:
>> https://issues.apache.org/bugzilla/show_bug.cgi?id=50486
>> 
>> --- Comment #37 from Ralph Goers <[email protected]> ---
>> Yes, that is what clear is for.
>> 
>> I don't see how Hierarchy.shutdown() can call MDC.clear(). Well, it could but
>> it would only clear the thread the shutdown is performed on which isn't 
>> likely
>> to help much.
>> 
>> Item 2 is likely to cause many problems.  MDC.clear only clears the current
>> thread's ThreadLocal. Reinitializing the MDC will cause problems for all the
>> other threads.
>> 
>> --
>> You are receiving this mail because:
>> You are the assignee for the bug.
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [email protected]
>> For additional commands, e-mail: [email protected]
>> 
> 
> 
> 
> -- 
> http://www.grobmeier.de
> https://www.timeandbill.de
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> 

Reply via email to