More Java posts
August 27, 2003
log4j - per thread logging

I"ve been trying to extend log4j so that a program can set the logging level for an individual thread. This is so that we can easily log the flow of a transaction from the web tier, through the ejb container, down to the database and back. Implementing a Logger subclass to use a ThreadLocal variable to hold the current logging Level was simple enough.

The difficult part was to get log4j to actually use the subclass. What I wanted was to be able to write Logger log = Logger.getLogger(Foo.class) and have my new subclass of Logger returned. It looked like it would simply be a case of implementing a new LoggerFactory and specifying it in the log4j config file. No such luck. The documentation describes how to do this in some places but says it's no longer supported in others. Experimentation indicates it's no longer supported. Looking at the code for log4j indicates there is now a much clever scheme suitable for JBoss etc. so that they can install there own logging systems.

Fortunately for what I want I can control the actual class used by simply writing Logger log = MyLogger.getLogger(Foo.class) and using the getLogger(String, LoggerFactory) call inside the getLogger method. Time for some python code for mass search and replace now!

Posted by Alex at August 27, 2003 09:02 PM
Comments
Post a comment
Name:


Email Address:


URL:


Comments:


Remember info?