22 December 2008

Configuring log4j for a SEAM based application in Tomcat

By default Tomcat uses JDK logging. I used the following in log4j.xml [which should go under WEB-INF\classes folder] to enable log4j for a SEAM based web application [infact also for any other web application] in Tomcat. In Maven please place it under, src\main\resources
<?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="CONSOLE" class="org.apache.log4j.ConsoleAppender">

<param name="Target" value="System.out"/>

<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">

<param name="File" value="${catalina.base}/logs/yourwebappname.log"/>

<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-5p [%c{1}] %m%n"/>
</layout>
</appender>

<root>
<priority value="INFO"/>

<appender-ref ref="FILE"/>
<appender-ref ref="CONSOLE"/>
</root>

</log4j:configuration>

Note: Please ensure log4j jar is available in the classpath. In a Maven based application add the below dependency to include log4j jar.
<dependency>
<groupId>apache-log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>