Getting Commons Logging/Log4J working under WAS 6.1

August 27, 2008

It seems like every time I turn around I run in to class loading issues when working with Java. Trying to get something as simple as logging working for an application can turn in to a real chore. This is especially true of getting Apache Commons Logging (aka. clogging) to work on WebSphere Application Servier (WAS). Since I haven’t yet found that there was a single, straight forward, step-by-step guide to doing this I decided to write this article.

The Issue

The basic problem with clogging under WAS is that WAS in fact uses clogging for its own purposes under the covers. In doing so they’ve configured it to use their own logging class (the WsJDK14Logger I believe). By default this will dump any clogging activity out to the console and be recorded in the stdout log file. This is far from ideal if you wish to isolate the logging activity for a single application in its own log file.

The Bitter Taste of Defeat

Regarding myself as somewhat of a class loading expert (in a novice kind of way), I somewhat foolishly thought “I know I’ll just dump the log4j.jar file and the commons-logging.jar file into my WEB-INF/lib folder and put a log4j.properties file in my src folder. Then I’ll install the application on the application server and set the module class loader to application first”. Simplicity itself I thought! What could be easier? Bah-bow – WRONG! It crashed and burned in the worst way possible: it gave me nothing. Absolutely bloody nothing. The application just merrily went on its way happily dumping its logging information straight into stdout.

Feeling rather bitter about the resounding defeat I’d just been handed I resolved to see what was out there on the web – which led to even greater despair. This issue is the subject of much confusion and misunderstanding with very few good answers. Luckily I happend to stumble upon this forum thread and an IBM presentation which combined told me approximately what I needed to know.

Seven Steps to Success

In a nutshell this is what you need to do to get this working inside a web project.

1. Import your commons-logging.jar and log4j.jar files into the EAR project in which the module will be bundled. You can do this by right-clicking on the EAR project and selecting Import > J2EE Utility Jar if you’re using RAD or Eclipse. Otherwise you can just copy these JARs directly into your EAR project and it should have the same effect.

2. Create a folder called services inside the META-INF directory of your web project. Inside this folder create a file called org.apache.commons.logging.LogFactory and copy and past the following into the file:

org.apache.commons.logging.impl.Log4jFactory

3. Add both the commons-logging and log4j JAR file to the MANIFEST.MF file located in the META-INF folder of your web project. For example:

Manifest-Version: 1.0
Class-Path: commons-logging.jar
  log4j-1.2.15.jar

4. Next if you’re using RAD open the EAR deployment descriptor and click on the deployment tab at the bottom. Then scroll down to the Applications section at the bottom of the page and change the classloader mode from PARENT_FIRST to PARENT_LAST. Save the file and close it. Note: if you can also do this once the application is installed from the WAS admin console.

5. Now make sure that you have your log4j.properties file configured and available somewhere on the class path inside your web app. Putting it at the root of the src directory is usually your best bet.

6. Install the application and restart the application server.

7. Profit.

Sorry, that last point was a complete waste of time but I needed another one to round out the seven. Anyway there you have it! Commons Logging running blissfully inside a WebSphere web container.

12 Responses to “Getting Commons Logging/Log4J working under WAS 6.1”

  1. martin Says:

    hi, nice blog. Can you tell me the name of the theme? I like it.

  2. David Kuhn Says:

    Thanks Marty. The theme is called “Freshy”, it’s one of the standard wordpress ones.

  3. raj Says:

    I tried the above said option but it didnt worked for me:(

  4. raj Says:

    Do we need to change anything at all on the app server? along with the above mentioned things? we treid setting the application class loader first and is there any other setting which we have to do?

  5. raj Says:

    Pls respond asa soon as possible. Thanks in advance..

  6. David Kuhn Says:

    Sorry Raj I don’t know how to help you mate. I only know the way I described above and it seems to work for me. If you need some extra background info I’d say go and check the source articles which I compiled this guide from. Maybe they can shed some more light. It’s a tricky business this one and it can take a few goes to get it right.

  7. Sue Trrustin Says:

    Thanks a million for this post!!! I tore my hair out all day yesterday trying to get log4j working on WAS via RAD and nada. Your post saved the day.

    Thanks a million!

  8. Michael Campbell Says:

    How would you amend this process if you have multiple WID (i.e. RAD) EJB projects and multiple WAR projects that all go into one EAR?

  9. Juan Says:

    1. Create and copy commons-logging.properties
    Create a file with the name commons-logging.properties. Fill it with this content:
    priority=1
    org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl
    org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
    Copy it to this folder:
    /opt/WebSphere7.0/AppServer/properties
    2. Copy commons-logging.jar y log4jx.x.xx.jar
    Copy the commons-logging and log4j jars in:
    /opt/WebSphere7.0/AppServer/lib
    3. Restart Websphere
    a. Enter this path
    /opt/Websphere7.0/AppServer/bin
    b. Execute
    ./stopServer.sh server1
    Where server1 is the name of your server. In my case the name was server1.
    c. Execute
    ./startServer.sh server1

  10. Juan Says:

    I forgot to comment my post. I was having the same problem. I could not make Websphere log in outside files. It only logged to SysOut.log and SysErr.log. It was making me crazy. I found this solution:

    1. Create and copy commons-logging.properties
    Create a file with the name commons-logging.properties. Fill it with this content:
    priority=1
    org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl
    org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
    Copy it to this folder:
    /opt/WebSphere7.0/AppServer/properties
    2. Copy commons-logging.jar y log4jx.x.xx.jar
    Copy the commons-logging and log4j jars in:
    /opt/WebSphere7.0/AppServer/lib
    3. Restart Websphere
    a. Enter this path
    /opt/Websphere7.0/AppServer/bin
    b. Execute
    ./stopServer.sh server1
    Where server1 is the name of your server. In my case the name was server1.
    c. Execute
    ./startServer.sh server1

  11. mahendran pandian Says:

    The below changes we need make to make the log4j working in websphere 7

    Step 1: change the class loading to parent last

    Step2:
    create a file
    MATA-INF/services/org.apache.commons.logging.LogFactory

    the content of the file should be as below
    org.apache.commons.logging.impl.Log4jFactory

  12. David Says:

    Have you a EAR sample ? i tried the steps, but don’t work for me


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: