2 Replies Latest reply on May 16, 2012 2:42 PM by jamezp

    AS 7.1.1 Log4j MDC doesn't work + log4j magic class loading

    griffin777

      Hello all,

      Is anyone able to explain how log4j class loading and bridging to jboss logmanager works? - I have a hard time understanding that.

       

      My problem started with not working Log4j MDC. I use it to store request id in my webservice application - it should be automatically logged via %X{rid} pattern but it isn't.

      I used debugger to check what happens under the hood.

       

      My code looks like this:

       

      private static final Logger LOG = Logger.getLogger("logger1");

       

      Then in a method:

       

      String requestId = ......;

      MDC.put("rid", requestId);

      LOG.info("Log something")

       

      Now I have a breakpoint in the method and I'm checking

       

      MDC.class.getClassLoader().toString()

      =

      ModuleClassLoader for Module "org.apache.log4j:main" from local module loader @469becb3 (roots: c:\geo\jboss\modules)

      the value shows that the class has been loaded by org.apache.log4j module classloader.

       

      Logger.class.getClassLoader().toString() - the same - the value shows that the class has been loaded by org.apache.log4j module classloader.

       

      ... but ...

       

      LOG is in fact an instance of the org.jboss.logmanager.log4j.BridgeLogger

       

      How can that happen when the org.apache.log4j module contains only the original log4j.jar (1.2.16) - I guess this is some kind of magic.

      But then the same magic is not done for the MDC class and when I call MDC.put method it runs the original log4j MDC method - my requestID is then stored in some log4j ThreadLocal that has nothing to do with jboss logmanager - so when jboss logmanager logs the message (remember that LOG is an instance of logmanager BridgeLogger) it can't see my requestID.

       

      It look like it's a bug or unsupported log4j MDC.

       

      Regards

      Piotr Zielniok