logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christian.H...@equifax.com
Subject RE: MDC copies
Date Wed, 04 Feb 2004 22:03:51 GMT
I'm not sure if the memento will work. Here is our scenario:

We have a map of parameters that need to go into MDC that we want to
print in the log as a block. So, lifecycle looks like this:

SharedContext.put("userId", "bob");
SharedContext.put("transactionId", "123");
logger.info("Something happened.");

Want log line to look like (this is simplified):

04 Feb 13:39 INFO my.Class (transactionId=123,userId=bob]) - Something

The main requirement that led me to not just put stuff directly in MDC
is that I have an unconstrained list of keys (so can't have a layout
like "transactionId=%X{transactionId},userId=%X{userId}"). Also, though
I could get around this if I dug into PatternLayout a little, I want a
custom "format" of the MDC portion...so in my case, I have a custom
toString() method in the map that I add to MDC.

I can also envision a scenario where I might want to format different
chunks differently, like: "codes:1,3,6,5,4 transactionId=5" where MDC
looked like: codes=<list of some codes that are added to over time>
transactionId=<simple value>

Ideas welcome.

-----Original Message-----
From: paul.smith@lawlex.com.au [mailto:paul.smith@lawlex.com.au] 
Sent: Wednesday, February 04, 2004 4:18 PM
To: log4j-dev@logging.apache.org
Subject: RE: MDC copies

> >Without this fix or something like it, any mutable content in MDC 
> >will be carried across threads and will be subject to 
> >ConcurrentModificationExceptions at the least and the logging of 
> >incorrect context data since the context can change between the time 
> >the event was issued and the asychronous append.

Putting something that is mutable into the MDC is something that I don't
think you really want to be doing is it?  Wouldn't it be better to be
storing an immutable Memento of the item's state in the MDC?  If the
item changes, isn't it easy to then replace the Memento in the MDC?  
Then if another Thread gets a copy of the MDC you won't have to worry,
since the Memento is immutable.

Could you give us some context as to why a mutable object is going into
the MDC? That might give us more clues.

To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org

View raw message