Rolling logs by both size and time

2019-01-24 11:00发布

I use RollingFileAppender of log4j 1.2.16, which rolls log files, when they reach a certain size. Now I would like to roll log files daily and when they reach a certain size. Thus there will be one or more log files per day.

For example,

myapp.log
myapp-17.12.2013.log
myapp-16.12.2012.log
myapp-16.12.2012.1.log
myapp-16.12.2012.2.log

Is there an off-the-shelf appender, which does it already?

3条回答
倾城 Initia
2楼-- · 2019-01-24 11:11

There are indeed two options:

  1. use LogBack with its size and time triggering policy: http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedFNATP
  2. there is TimeAndSizeRollingAppender for Log4J from here: http://www.simonsite.org.uk/

Keep in mind that both options use file renames. Consider this carefully if there's another script automatically moving these files. File rename is risky when two processes deal with the same file.

My suggestion is to directly write to immutable log file name in the pattern: myapp-{dd.MM.yyyy}.{X}.log. That way "rolling" is simply closing one file and opening a new one. No renames. No background threads.

查看更多
一夜七次
3楼-- · 2019-01-24 11:25

The quick answer is "no". Looking at log4j's javadoc: https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/FileAppender.html

There are only two out-of-the-box file appenders: DailyRollingFileAppender and RollingFileAppender (and the first one is not recommended because it has synchronization issues).

To achieve what you want, you should create your own appender, extending RollingFileAppender and modifying it to roll the file if the day changes. The modification would be in method:

 protected void subAppend(LoggingEvent event)

You can see its source here: http://www.docjar.com/html/api/org/apache/log4j/RollingFileAppender.java.html (line 274).

You just need to copy and paste the code and change the if calling rollOver to suit your needs.

查看更多
劫难
4楼-- · 2019-01-24 11:33

Below configuration xml will do the job: JAR required: log4j-rolling-appender-20150607-2059

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
    xmlns:log4j='http://jakarta.apache.org/log4j/'>
    <appender name="file"
        class="uk.org.simonsite.log4j.appender.TimeAndSizeRollingAppender">
        <param name="File" value="D:\\App.log" />
        <param name="Threshold" value="DEBUG" />
        <param name="DatePattern" value=".yyyy-MM-dd" />
        <param name="MaxFileSize" value="1KB" />
        <param name="MaxRollFileCount" value="100" />
        <param name="ScavengeInterval" value="30000" />
        <param name="BufferedIO" value="false" />
        <param name="CompressionAlgorithm" value="GZ" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p %-23d{ISO8601} [%t] %x: %c{1} - %m%n" />
        </layout>
    </appender>

    <root>
        <level value="DEBUG" />
        <appender-ref ref="file" />
    </root>

</log4j:configuration>
查看更多
登录 后发表回答