Tech Crawler

crawls with java life

Log – using log4j

Note:This article is aimed at people who face issues in configuring log4j in various environments like in a standalone program, web application , Enterprise application etc.

If you are at this page ,  and have continued reading the post then i presume that you should be a new bee to java and probably wondering/facing issues in configuring the log4j  for your application.

A quick intro…

Log4j is Open Source Utility developed under Jakarta Apache project.If you are looking for information on what/how and to know the basics of log4j there plenty good articles to refer to.

Below are some articles that i read to have a fair understanding

Log4j Article – Onjava.com

Now that you have the satisfaction of  deploying and configuring the sample application available in above links ,I seriously recommend you to fully go through the article available on the official Site

Log4j- Official Site

Lets hope that you had gone through the previous articles , on would think configuring Log4j is pretty straight forward as configuring some files and putting it in a right place and its true.But most make bland  mistakes when it comes to simplest of things and on a bad day breaking our head ,pondering  why the logging is not working.

To find out why and how to solve your logging issue i would like you to see this article

Understanding Classloaders: log4j in a J2EE Environment

Below is some of the pointers one has to check/do for configuring log4j in various environments.

Log4j- Standalone Program:

  1. Make sure the log4j.jar and the log4j.properties are  in the classpath of the environment.

Configuring A Windows Working Environment

Configuring A Unix Working Environment.

Log4j- integration with WAR:

  1. In the war the log4j.properties should be in WEB-INF/classes
  2. Make sure ,log4j.jar is in WEB-INF/lib directory

Log4j-integration with EAR (containing multiple Wars , different log files)

  1. Create Wars and package as mentioned in the above section(Log4j- integration with WAR) i.e each WAR having its own Log4j.jar and log4j.properties.
  2. Also make sure there aren’t any Log4j.jar in the root of the ear

This would create individual log files for each of WAR in the EAR.

(this configuration was tested in tomcat 4 – 5 /Weblogic 8.1 – 9.2)

In some organizations,log4j.jars and other common jars are available in the shared lib of the server and added to the classpath when the Server is starated. In that case we need to force the weblogic (by include weblogic.xml to the WEB-INF in the WAR) to use the  jar within the war ,otherwise both WAR would log on the same file.

Log4j-integration with EAR (containing multiple Wars , different log files)

  1. Place WARs and log4j.jar and log4j.properties in the root location.

This configuration works like a gem in TOMCAT but if you try to deploy it in the Weblogic environment , the application doesnt genrate log files.This is because as far as weblogic only modules like EJbs and WARs should be at the root level of the EAR.

Log4j-integration with EAR (containing multiple Wars , logging to a single file)

  1. Place all the common libs including the log4j.jar in the APP-INF/lib
  2. Place the common log4j.properties in the APP-INF/classes

viola the weblogic will start spitting the logs

Log4j-integration with EJB

Generally the EJBs  would be deployed in the Application Server as EAR as most EJBs would use many jars and manier cases even along with the WARs which would also be packed in the EAR (atleast thats how it is for all of the projects that i had worked for)

In this case , most of the times it would be a requirement to make the WARs and EJBs in the EAR log to a same file.

  1. Do all of the steps mentioned in the section  Log4j-integration with EAR (containing multiple Wars , logging to a single file)
  2. Above step would make all of the classes in the WARs log, but to make EJB to log to the same file , you should make following entry in the manifest.mf file in the EJB.jar file

Class-Path: log4j.jar


January 3, 2008 Posted by | Uncategorized | , , , , , | Leave a comment