Using Commons-Logging 1.1 with Log4j on WebSphere 6.0

Log4j does seem to work out of the box with Websphere 6 so I cobbled together some tips and advice from the web and wrote this mini guide. It was a couple of years but hopefully be useful to someone.

WebSphere uses commons-logging and so it’s in the root classloader. In addition, WebSphere 6.0 ships a commons-logging.properties with the following properties set:

org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl

org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger
Therefore by default the application will use JDK logging and not log4j. The solution is to ensure that the right classloader mode is set and the application has an appropriate commons-logging.properties and use the official released version 1.1 of commons-loggin api:

Set application classloader mode as PARENT_LAST. This can be changed in eclispes EAR file deployment descriptor menu. This tells websphere to load the parent files last giving the application files priority. (the parent being the websphere root)

Also, create a new commons-logging.properties to the application classpath with the following entries:

priority=1
org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl
The priority flag was introduced in Commons Logging 1.1 to allow an ordering based on priority. To ensure that an application’s commons-logging.properties will take precedence over WebSphere’s file, a priority of greater that 0.0 must be set.  I changed mine to 5 (just to be sure 😉 ).

So basically the  commons-logging.properties overrides the one packaged in websphere and returns it to the default logging system which is log4j.

NOTE: You also have to replace the commons-logging-api.jar that comes supplied with websphere, because IBM use the API and which doesn’t contain the log4j logger implementation. 🙁

Replace

<Rational Instal DIR>\Rational\SDP\6.0\runtimes\base_v6\lib\commons-logging-api.jar
With the commons-logging-api.jar downloaded from http://jakarta.apache.org/site/downloads/downloads_commons-logging.cgi

You should also import include it in your go to project properties, java build path and import external jar. Ensure it’s imported above the websphere runtime library otherwise it’ll read the other one first. You can change the order of the imports in project prop > java build path > order and export.

LOG4J Config
To configure log4j you’ll need to create a log4j.propertuies file or a log4j.xml file. This can be located in the application classpath (same level as the ‘com’ directory). Ideally this should be located outside of the EAR/WAR file, where it can be easily editing whilst the application is running without the need to redeploy.

Note: When you build the project both log4j.properties and commons-logging.properties should be copied to the WebContent>Web-Inf>Classes

Checklist:

  • Created commons-logging.properties and log4j.properties in java classpath.
  • Change application classloader mode as PARENT_LAST
  • Import the commons-logging 1.1 jar file
  • Ensure imported jar in imported before Websphere runtime
  • Clean, Build and redeploy (also restart websphere)

More info:
http://wiki.apache.org/jakarta-commons/Logging/FrequentlyAskedQuestions
http://www.webagesolutions.com/knowledgebase/waskb/waskb004/index.html
http://www.mobilefish.com/developer/log4j/log4j_quickguide_examples.html