[slf4j-user] Why is org.slf4j.NDC in the JavaDocs?

scott.tabar at fuse.net scott.tabar at fuse.net
Fri Jul 10 22:49:38 CEST 2009


Thanks Ceki for the information.  That explains a great deal.

As noted in prior threads, the sfl4j implementation that uses MDC becomes painfully slow.  Pushing to a depth of 100,000 then popping them all, it takes sfl4j's implementation about 2554.312 seconds to run!  I rewrote the NDC implementation, which still is using the sfl4j's MDC, to tweak more speed out of it and I am getting an average time of about 0.097 seconds for 100,000 pushes then pops!  Not bad.

But one HUGE problem I am running in to with slf4j's NDC which is backed by the MDC is that the log4j pattern %x is not being picked up by log4j's LoggingEvent.getNDC() which performs the %x pattern substitution.

Do you or anyone have "any" ideas on how to get %x to pick up the NDC?  If it cannot, then honestly, what is the real point of backing the slf4j NDC with a MDC when you cannot hit patterns?  Off hand I don't think you can use wild-cards in a MDC pattern such as %X{NDC*} which will be a work around, but then it kinda breaks the whole log4j-should-have-no-knowledge-of-slf4j.

Reviewing the code for slf4j MDC it looks like it would be fairly trivial to just create a similar wrapper for NDC that is backed by log4j's NDC.  I just might go ahead and do that.  If I do would anyone be interested in bringing the code base in to slf4j?  I guess I don't understand the argument for not implementing NDC other than the person who might have been requesting it would not have been the one writing it and hence a lack of time and interest in whom ever could be found holding the bag so to speak.  If there is a good reason for not implementing a NDC backed by log4j, such as how MDC is done, then please let me know.

I'll post the details on the performance tweaks and stats on NDC (log4j vs. slf4j vs. myTweak) later.  Needless to say, log4j blows chunks when you try to push between 15,000 and 20,000 items with an average message length of about 5.  It dies from an OutOfMemoryException even with a max heap size of 1.2 gigs!  The root cause is that NDC is using an inner class named DiagnosticContext where the original message is being stored, along with the prepended messages from all parents!  So the storage requirements becomes a factorial problem!  It is this "parent" message glob that ultimately gets used in the %x pattern substitution. 

So thank to one and all for any knowledge on getting %x pattern to work with a MDC!

My apologies if some of this content would be more appropriate for the developers thread... I should join that next... 

   Scott Tabar

---- Ceki Gulcu <ceki at qos.ch> wrote: 


scott.tabar at fuse.net wrote:
> Greetings,
> 
> NDC in log4j is working great for me.  I really don't want this thread to degrade in to why MDC may be better; I'd like to have this question answered first.
> 
> I was very happy to see it in the slf4j JavaDocs:
> http://www.slf4j.org/api/org/slf4j/NDC.html
> 
> But when I tried to actually use it, it was then when I found out that the class really does not exist.  At least not in v1.5.8 (or going back to v1.5.2).

NDC is included in the slf4j-ext module (slf4j-ext.jar). It is
implemented in terms of MDC. The fact that NDC is in the org.slf4j
package can be confusing. Sorry about that.

If NDC over MDC meets your needs and you wish to see it included in
the slf4j-api module, then please do enter a bug report.

>   Thanks for any and all answers.
> 
>      Scott Tabar
> 

-- 
Ceki Gülcü
Logback: The reliable, generic, fast and flexible logging framework for Java.
http://logback.qos.ch
_______________________________________________
user mailing list
user at slf4j.org
http://www.slf4j.org/mailman/listinfo/user




More information about the slf4j-user mailing list