First of all, I never use Jakarta Commons Logging (JCL) directly in my applications (Log4J is my tool of choice for logging tasks), but as you probably know many frameworks and libraries do all of their logging through JCL. In most cases you will want to catch some of these log messages, so that you can better monitor your application. For example, you will probably want to log all SQL queries that Hibernate issues, at least in the early development phase, so that you can have a better perspective of what is going on.

I usually don’t have a problem with JCL since it finds a Log4J implementation in the classpath, get my configuration and all works fine. Except when it doesn’t.

In this particulair case, I had a Spring-Hibernate based web application that I needed to deploy on the Tomcat 5 server. After deploying an application, commons logging couldn’t find a log4j implemantation and all logs were redirected to the standard Tomcat log file.

Your first reaction will probably be: “This one is easy, just put you log4j jar in the server classpath (commons/lib folder probably) and all should be fine”. While it should be a no-brainer task, it is a bit more complex then that.

The root of the problem is that Tomcat 5 has a commons-logging-api.jar file in the bin/ folder. This jar is hardcoded in the booting procedure and loaded during the Tomcat boot time in order to be used for server logging tasks. All attempts to use some other JCL classes will be ignored by Tomcat. The problem is caused by the fact that this version of JCL does not contain a log4j addapter.

Even though JCL troubleshooting section says that putting appropriate JCL and Log4J jars in the WEB-INF/lib folder should work, it doesn’t.

After some searching and experimenting I managed to find a solution that works for me:

  • First of all, we need to replace commons-logging-api.jar in the bin/ folder with the full JCL implementation. Note that jar filename must be commons-logging-api.jar, since it is hardcoded in the Tomcat boot routine. Also, be sure that you use version 1.0.4 (or newer) or you will have problems in trying to stop your application.
  • The second step is to put an appropriate version of the log4j jar in the common/endorsed folder.

Now it should be working in the same manner as on all other servlet containers. I’m interested if anyone else had some similar expiriences and what techniques were used to soved these kinds of issues. Also, it is a shame that this is not properly documented.