views:

84

answers:

1

In my program I have 2 separate streams of logging events (calling them streams for simplicity, in reality its 2 appenders). Stream1 contains client logging and Stream2 contains control logging. Now this might seem easy, except that certain classes can be both in the client logging and server logging, depending on the situation. Complicating this further is the fact that a command that a client wants takes place in 2 separate threads (one being fetched randomly from a thread pool), so any kind of tracking with MDC or NDC isn't possible.

What would really simplify this is if the logger could inherit the appenders from the calling instance. That way I can setup 2 appenders for 2 loggers and be done. However I have no idea how to do it cleanly or easily. Can anyone offer any advice on how to do so?

Note: If something needs to be passed around, I do have a event bean that gets passed to everything in the chain that can be used if necessary.

+1  A: 

You've already mentioned that the client processing takes place in multiple threads, so a simple ThreadLocal approach might not work... but would a ThreadGroupLocal work?

See [http://stackoverflow.com/questions/2017155/are-there-thread-group-local-variables-in-java]

The tact I am suggesting is to only have one appender registered with the logging framework. That appender would be something you write. The implementation would delegate to the ThreadLocal/ThreadGroupLocal appender. The that appender would be client- or control-specific.

Also note that your appender should not be configured beneath asynchronous or batching abstractions.

Dilum Ranatunga
That seems like I would be creating a pool for each client request, which destroys the point of pooling. If not, then I would have to create a very complex logging system with each thread having a MDC setup. Technically possible, but absolutely ridiculous.And my hopes of using a stack trace were shot down today as I found the trace doesn't go back to the calling class, not surprisingly. Think its time for that custom thread pool I've been thinking of writing.
TheLQ
Coming back I'm going to give credit where its due. I found a ThreadGroupLocal class on the web and used a single appender that checked the local variable. It works great, and I just want to say, Thanks!
TheLQ