Is there something I can add to the logback pattern layout to log the bundle name?
Background: I have a shared class that is instantiated as a bean within the Spring contexts of several bundles. When this bean logs an Exception it is unclear as to which bundle the exception occurred within. The ideal approach would be to have the logging system tell me this automatically rather than adding information to the log message manually.
I think it would be necessary to add the bundle identity (bundle symbolic name and version or possibly bundle id) to Logback's Mapped Diagnostic Context before it would be possible to refer to the bundle identity in an encoder pattern.
This could be done, but I'm not convinced that Virgo should incur that overhead on every logging call.
An alternative would be to build logic into the exception constructor to derive the bundle name and add it to the exception message. You could examine the stack and then use FrameworkUtil.getBundle(Class< ? >) to get the Bundle from the appropriate class in the stack.
It probably would but only for exceptions, and only those exceptions which I specifically instrumented. What would be better is a generic solution that would work for all logging messages.
What do you think about the approach I described? The only overhead would be the MDC put and release calls on each service call. Unfortunately, it won't cover every situation -- for example, code that runs outside the context of a service call, but it would help nonetheless.
Frieder Heugel Messages: 61 Registered: October 2010 Location: Basel, CH
maybe using AspectJ would be a solution for your problem. You could write an aspect that is intercepting every log call or catching any exception. Then you could add the needed information and continue with the regular program.
Using Aspects sounds like a reasonable interim approach.
I guess in the long run though this sort of thing will need to be part of the toolset. Not being able to determine out of the box which bundle logged a message is a big information deficit, and I'm surprised this isn't a common request.
Fair enough -- this situation has come up for me in cases where multiple service bundles access shared utility bundles, and the log statements in the shared utility bundles need to list the important contextual information, which is the service bundle.
But in that case, a baked in solution wouldn't know which bundle to log anyway, so we are back to the explicit aspect/MDC solution. Makes sense.