Tomcat 6 and log4j Logging is Not Really Complete

WebDevel.jpg

I have been messing with Tomcat 6 and log4J logging today because this web app I've been working with had to add log4j for an external dependency that was added today. In general, I'm a fan of log4j if you go through the effort of using it properly, it's not bad. But adding it turned out to be a pain that I couldn't have imagined. In the end, I should have gone directly to the Tomcat 6 website and looked at the docs there and not questioned it. But I had done this before, and I was convinced that I could whip this up without any grief. (Ha)

The simplest thing was to create a log4j.properties file like:

  log4j.rootLogger=INFO,A1
      log4j.appender.A1=org.apache.log4j.ConsoleAppender
      log4j.appender.A1.layout=org.apache.log4j.PatternLayout
      log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

and place it in the WEB-INF/lib directory of the web app. I also placed the log4j.jar (version 1.2.15) into the same directory, deployed it and it worked. Almost.

Actually, it worked about half the time. The other half the time I got a warning from log4j saying it couldn't find the properties file. Very odd. If I edited the file, it was there and OK, but sometimes it'd work and sometimes not.

I looked into this from a lot of angles, and there was nothing I could do to get it to reliably recognize the log4j.properties file in the WEB-INF/lib directory of the web app. Then I read this from the Tomcat 6 website.

Seems they are saying that you have to place the log4j.properties file in the $CATALINA_HOME/lib directory. No choice. That means it's the same logging for all web apps on that instance. In general, I can see why that's required, but then I'm a little blown away that it wasn't working where it was. But it was... and it wasn't.

So in the end, I put the files (jar and properties file) into the $CATALINA_HOME/lib directory and restarted things and it's working fine. Kind of a pain to tease me with the possible working within the web app, and that would be exceptionally cool, but it's just not going to happen in this release of Tomcat 6.