I want to configure the following things for log4j:
- rotate logfile daily
- max logfile size 100MB (rotate when the log reaches the limit).
- compress daily rotated logfiles
- only keep compressed logfiles for 7 days.
- log should be written with a certain pattern
- log level info
preconditions: OS is windows, log4j version 1.1.x, xml configuration format
This is what I have:
<?xml version="1.0"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="RollingAppender" class="org.apache.log4j.rolling.RollingFileAppender">
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="ActiveFileName" value="logs/app.log"/>
</rollingPolicy>
<layout class="org.apache.log4j.EnhancedPatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%t][%c{1}:%L] %m%n" />
</layout>
</appender>
<root>
<priority value="INFO" />
<appender-ref ref="RollingAppender" />
</root>
</log4j:configuration>
Even if you use
apache-log4j-extras
(as it seems), it is not possible to obtain such a configuration out-of-the-box. What you lack is aTriggeringPolicy
that causes a rollover every day: theTimeBasedRollingPolicy
works correctly only if it is used as both theRollingPolicy
andTriggeringPolicy
. Otherwise it triggers a rollover every message.Therefore you need to create a class like this:
and transform your configuration to look like this:
Remark: As you probably know, Log4j 1.x reached its end-of-life 6 years ago. Its successor Log4j 2.x has all the triggering policies you need (cf. documentation), if you are willing to upgrade.