Sunday, September 9, 2007

Log4j.xml file instead of properties file

If for some reason you prefer to configure log4j in xml instead of usual properties file, I found out that you can do it very easy.

1. Create log4j.xml and put it in your apps class path, usually under web-inf.

I created simple log4j.xml as follows

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<!-- *#***We can also have XML file in class path instead of-->
<!-- properties file or you can put it where ever you want-->
<!-- and write a servlet and map it in web.xml -->

<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${TOMCAT_HOME}/logs/server.log" />
<param name="Append" value="true" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<!-- The default pattern: Date Priority [Category] Message\n -->
<param name="ConversionPattern" value="%d %-5p [%c] %m%n" />
</layout>
</appender>

Thats it. You can also put it anywhere in your app by doing this as mentioned in log4j manual here http://logging.apache.org/log4j/1.2/manual.html under Example Configurations -> Initialization servlet

2 comments:

Becky Cheung said...

Wow.

Anonymous said...

This is great info to know.