Rolling logs by both size and time

我的梦境 提交于 2019-11-29 04:14:47

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.

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.

brajesh kumar

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!