Configuring log4j logging in a web application

Objective:
To configure logging in a web application running in Tomcat using log4j.

Explanation:
Requirement is to create two log files, one for logging logs from the entire application and other from logging the log from a specific file. So the following example covers how to separate log of a specific class or package.

Prerequisite:
– Tomcat 6 up and running.
– A well structured web application.
(I have used eclipse for that and created a dynamic wed application.)
– Create an XML file under WEB-INF\classes folder named, log4j.xml.
– Grab the log4j jar (log4j-1.2.13.jar in my case) and place that in WEB-INF\lib

LET US START:

1. Copy paste the following in the log4j.xml and google to understand that well.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">


<appender name="FIRST">
<param name="File" value="${catalina.home}/logs/techmusings1.log" />
<param name="Append" value="true" />
<layout>
<param name="ConversionPattern" value="%d{dd MMM yyy HH:mm:ss}, %-6p %C:%M:%L %m %n" />
</layout>
</appender>


<appender name="SECOND">
<param name="File" value="${catalina.home}/logs/techmusings2.log" />
<param name="Append" value="true" />
<layout>
<param name="ConversionPattern" value="%d{dd MMM yyy HH:mm:ss}, %-10p %m %n" />
</layout>
</appender>
<!-- Configure the root appender -->
<root>
<appender-ref ref="FIRST"/>
</root>
<!-- Configure the category and log level for A -->
<category name="com.samples.technicalmusings">
<priority value="DEBUG" />
<appender-ref ref="FIRST" />
</category>
<!-- Configure the category and log level for B -->
<category name="com.samples.technicalmusings.logging.B">
<priority value="INFO" />
<appender-ref ref="SECOND" />
</category>
</log4j:configuration>

*You can also use log4j.properties as an alternative but the XML approach is more flexible.

2. Create following classes to put loggers in them:

public class A {
private static final Logger logger = Logger.getLogger(A.class);
public void log(){
logger.info("class A is logging info message.");
if(logger.isDebugEnabled()){
logger.debug("class A is logging debug message.");
}
}
}

public class B {
private static final Logger logger = Logger.getLogger(B.class);
public void log(){
logger.info("class B is logging info message.");
if(logger.isDebugEnabled()){
logger.debug("class B is logging debug message.");
}
}
}

3. Create a listener to call the above classes to fulfill their purpose when the server is initialized.

public class LoggingListener implements ServletContextListener {
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
public void contextInitialized(ServletContextEvent arg0) {
A a = new A(); a.log();
B b = new B(); b.log();
}
}

4. Configure the listener in web.xml by adding the following

<listener>
<listener-class>com.samples.technicalmusings.logging.LoggingListener</listener-class>
</listener>

5. Deploy the application in tomcat and start tomcat.

6. In $TOMCAT_HOME/logs directory you will find techmusings1.log and techmusings2.log

Output:

techmusings1.log

26 Sep 09 15:12:37, INFO   com.samples.technicalmusings.logging.A:log:10 class A is logging info message.
26 Sep 09 15:12:37, INFO   com.samples.technicalmusings.logging.A:log:10 class A is logging info message.
26 Sep 09 15:12:37, DEBUG  com.samples.technicalmusings.logging.A:log:12 class A is logging debug message.
26 Sep 09 15:12:37, DEBUG  com.samples.technicalmusings.logging.A:log:12 class A is logging debug message.
26 Sep 09 15:12:37, INFO   com.samples.technicalmusings.logging.B:log:10 class B is logging info message.
26 Sep 09 15:12:37, INFO   com.samples.technicalmusings.logging.B:log:10 class B is logging info message.

techmusings2.log

26 Sep 09 15:12:37, INFO       class B is logging info message.

Here you will see that in techmusings2.log only messages corresponding to class B have been printed. Also note that the debug level log is absent in techmusings2.log file. Find the reason for this in log4j.xml OR set the debug=”true” in log4j:configuration element in log4j.xml and monitor the logs of tomcat to know more.

You can download the above code from the following location:

http://technicalmusings.googlecode.com/svn/trunk/java/examples/logme

Enjoy Learning,

Kamlesh

Advertisements

6 thoughts on “Configuring log4j logging in a web application

  1. hi kam

    a superlative article
    you are blossoming into a Java Guru day by day
    i feel proud of you and tell stories of you to my friends
    and you will not believe
    you are inspirations to many ppl. now

    njoy baby

  2. mylemons,

    I agree with your suggestion and will incorporate that wherever necessary.

    Mow,
    Thanks for your overwhelming response. I am not aspiring to be a Java Guru. I am just using this forum to compile all my learning as I progress in my career.

    – Kam

  3. hi kamlesh
    nice to have your r&d with log4j, for persons like me, till date it was just a tool to find bugs :).
    i must say its gr8 to see u succeeding.

  4. I don’t know If I said it already but …I’m so glad I found this site…Keep up the good work I read a lot of blogs on a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say GREAT blog. Thanks, 🙂

    A definite great read..Jim Bean

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