15
Oct.
2003
Writing into multiple files with log4j
Using log statements for low-tech debugging is generally a good thing. Using only one file however may become chaotic especially if you want to trace third party frameworks as well. I faced the problem during the development of my system. When I set the threshold in log4j to DEBUG my logfile was exploding (Struts and Tiles are very talkative).
Thanks to log4j it is possible to use different files for different packages (and their children). First you have to declare the appenders you want to use, e.g. an appender called HIBERNATE:
<appender name="HIBERNATE" class="org.apache.log4j.FileAppender">
<param name="Append" value="false"/>
<param name="File" value="${server.home}/logs/log4j-hibernate.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
Afterwards you can attach a category to these appenders. This way you can log all statements from Hibernate into one log file, all statements from Struts into another log file and so on:
<category name="cirrus.hibernate"> <priority value="DEBUG" /> <appender-ref ref="HIBERNATE"/> </category>