So log4j comes with two existing log rollers: RollingFileAppender, and DailyRollingFileAppender. Has anyone heard of an appender that does both of what the former do?
I need an appender that will roll log files based on filesize, but also append the current date to it.
I've been thinking about creating my own appender, but if there is already one that has been created, why not save the time and use that one?
Looks like you want a mix of the http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/DailyRollingFileAppender.html and the http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/RollingFileAppender.html.
You'll have to code by yourself. The good news is: you'll just have "merge" those classes functionality, no "low level" new code required.
I know this post is a bit late but you could try out the TimeAndSizeRollingAppender. It's freely available under the Apache 2.0 license, download it from www.simonsite.org.uk.
The Log4J Extras from the "companions" project provide an array of policies for rolling, including what you're looking for.
APIDoc: http://logging.apache.org/log4j/extras/apidocs/index.html
Homepage: http://logging.apache.org/log4j/extras/
Too late to reply. But hopefully this will help someone.
Add log4j dependency to your pom.xml file
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
Grab a copy of this FileAppender implementation. Thanks to the author of this file.
http://haobangshou.googlecode.com/svn/trunk/hbs/APPLICATION/server/common/src/com/hbs/common/appender/TimeSizeRollingFileAppender.java
Place this log4j.xml file in your resources folder
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} [%p] %c{1} %m%n"/>
</layout>
</appender>
<appender name="debug" class="com.myproject.log.TimeSizeRollingFileAppender">
<param name="File" value="log/debug.log"/>
<param name="MaxBackupIndex" value="300"/>
<param name="Encoding" value="GB2312"/>
<!--CHANGE THIS TO A LARGER SIZE EG : 20MB. USE 1MB TO TEST IF THE SETTING WORKS.-->
<param name="MaxFileSize" value="1MB"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %p [%c] - [%m]%n"/>
</layout>
</appender>
<!--CHANGE name TO ROOT PACKAGE NAME OF YOUR PROJECT THAT YOU WANT TO LOG.-->
<logger name="com.myproject" additivity="false">
<level value="debug"/>
<appender-ref ref="consoleAppender"/>
<appender-ref ref="debug"/>
</logger>
<logger name="org.springframework" additivity="false">
<level value="debug"/>
<appender-ref ref="consoleAppender"/>
<appender-ref ref="debug"/>
</logger>
<logger name="org.hibernate" additivity="false">
<level value="debug"/>
<appender-ref ref="consoleAppender"/>
<appender-ref ref="debug"/>
</logger>
<root>
<priority value="INFO"></priority>
<appender-ref ref="consoleAppender"/>
<appender-ref ref="debug"/>
</root>
To do a quick test:
import org.apache.log4j.Logger;
public class Main {
static public void main(String[] args) {
Logger log = Logger.getLogger(Main.class);
for(int i = 0; i < 10000; i ++)
log.info("Testing log");
}
}
Done!!
Using Log4j
As @JavaJigs said, Log4j's extras can be used.
First of all, if you're using Maven, add it as dependency keeping in mind to use the same log4j version, just to avoid any kind of conflict.
<!-- you should already have something like this -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- add this one, please note the version is the same than log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>apache-log4j-extras</artifactId>
<version>1.2.17</version>
</dependency>
Then, set up an appender to use both kinds of rollings. This is a quick and dirty example that rolls every minute and / or when the size of the log files exceeds 1000 bytes.
<appender name="rollout5" class="org.apache.log4j.rolling.RollingFileAppender">
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="ActiveFileName" value="log4j-current.log" />
<param name="FileNamePattern" value="log4j-%d{HH-mm}.%i.log.gz" />
</rollingPolicy>
<triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
<param name="MaxFileSize" value="1000" />
</triggeringPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n" />
</layout>
</appender>
Add the appender to the logger as you usually do.
If you run the application you should obtain something like that...
log4j-current.log
log4j-18-13.1480266729211.log.gz
log4j-18-12.1480266729095.log.gz
log4j-18-12.1480266729123.log.gz
Obviously the numbers of files and their names depend on how your application logs.
As you can see, the %i
placeholder gets replaced with a sort of random yet ever increasing number. I was not able to find a way to have it replaced with number starting from 0 though. Nevertheless, such files listed in alphabetic order should match their historical order.
Other ideas
I know you mentioned explicitly log4j. But, if you can, why not to evaluate moving to log4j2? In this log4j2 piece of doc there are a couple of examples that seem to suit your needs.
We use the class you see here. It works as you described and extends FileAppender.