Monday, April 14, 2008

Log4j Root Logger

I just found out that with log4j I can set-up a "root" logger. This is a catch-all logger that writes everything, regardless of the package. Seems pretty obvious, but I hadn't seen it before. If you use this in conjunction with a package logger, and you are outputting to the same file, then use the additivity="false" attribute on the package logger to stop lines appearing twice. My example log4j.xml file is below. Note that I output everything to stdout when developing, and then switch over to the rolling log file when I deploy it.


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<!-- Print the date in ISO 8601 format -->
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="/var/tmp/logs/myapp.log" />
<param name="MaxFileSize" value="10000KB" />
<param name="MaxBackupIndex" value="3" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<logger name="org.apache.fop" additivity="false">
<level value="info" />
<appender-ref ref="stdout" />
</logger>
<logger name="com.example" additivity="false">
<level value="debug" />
<appender-ref ref="stdout" />
</logger>

<!-- root logger -->
<root>
<level value="info" />
<appender-ref ref="stdout" />
</root>
</log4j:configuration>


1 comment:

padmalekha said...

Searched the web for 1 hr how to do this. Finally your posting helped. Thanks.