Below shows how to configure logging using log4j.

Sample Logging File:

RollingFileAppender extends FileAppender to backup the log files when they reach a certain size.

<appender name=”Rollfile”
<param name=”maxFileSize” value=”2KB” />
<param name=”maxBackupIndex” value=”5″ />
<param name=”File” value=”${catalina.home}/logs/SimpleWebApp.txt” />
<param name=”threshold” value=”debug” />
<layout class=”org.apache.log4j.PatternLayout”>
<param name=”ConversionPattern”
value=”%d{ABSOLUTE} %5p – %m%n” />

DailyRollingFileAppender extends FileAppender so that the underlying file is rolled over at a user chosen frequency. DailyRollingFileAppender has been observed to exhibit synchronization issues and data loss. The rolling schedule is specified by the DatePattern option. This pattern should follow the SimpleDateFormat conventions.

For example, if the File option is set to /foo/bar.log and the DatePattern set to ‘.’yyyy-MM-dd, on 2001-02-16 at midnight, the logging file /foo/bar.log will be copied to /foo/bar.log.2001-02-16 and logging for 2001-02-17 will continue in /foo/bar.log until it rolls over the next day.

<appender name = “DailyFile”
class = “org.apache.log4j.DailyRollingFileAppender”>
<param name = “File” value = “${catalina.home}/logs/DailyFile.txt” />
<param name = “DatePattern” value = “‘.’yyyy-MM-dd”/>
<param name = “Append” value = “true” />
<param name=”threshold” value=”info” />
<layout class= “org.apache.log4j.SimpleLayout” />

The AsyncAppender lets users log events asynchronously. The AsyncAppender will collect the events sent to it and then dispatch them to all the appenders that are attached to it.
<appender name=”ASYNC”
<appender-ref ref=”TEMP”/>
<appender name=”TEMP” class=”org.apache.log4j.FileAppender”>
<param name=”File” value=”temp”/>
<layout class=”org.apache.log4j.PatternLayout”>
<param name=”ConversionPattern” value=”%d %-5p [%t] %C{2} (%F:%L) %m%n”/>