Log4j2 auto rollover after specified duration

我与影子孤独终老i 提交于 2019-12-06 11:04:43

问题


I am using RollingFile appender. I want the log file to be rolled after every 20 minutes irrespective of the logging event. For instance in an hour I should have 3 log files even though there might have not been any logging in that hour. Is this possible using Log4j2? If yes please provide the configuration ( in log4j2.xml) that are required. The below config does not seem to work :

       <RollingFile name="RECHARGE_NMCD" fileName="D:/rc_nmcd/rc_nmcd.log" append="true" bufferedIO="false" filePattern="D:/rc_nmcd/rc_nmcd_%d{yyyy-MM-dd-HH-mm}.process">
            <PatternLayout>
                <Pattern>%m%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="20"/>
            </Policies>
            <DefaultRolloverStrategy max="20" />
        </RollingFile>

回答1:


I don't think you can make Log4J2 roll every N minutes out of the box, it looks like you can get it to do this every minute, hour, day but not 20 minutes. (See https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/DailyRollingFileAppender.html - you can change it to "every minute" using a different date pattern)

I've not tried this, but there might be a way of customising this by providing a custom Rollover Strategy...

https://logging.apache.org/log4j/2.x/log4j-core/apidocs/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.html

If this works, please post your answer for other people to learn from!




回答2:


We may can do this using Corn Expression policy

CronTriggeringPolicy schedule="0 0/20 * 1/1 * ? *"/>.

This will roll your file automatically every after 20 minutes, irrespective of logging event.




回答3:


try this :

<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>foo.%d{yyyyMMdd-HHmm}.gz</fileNamePattern>
      <cleanHistoryOnStart>true</cleanHistoryOnStart>
      <maxHistory>20</maxHistory>
</rollingPolicy>

instead of

<Policies>
   <TimeBasedTriggeringPolicy interval="20"/>
</Policies>



回答4:


I referred this plugin https://github.com/mushkevych/log4j2plugin

I had a Runnable thread per FTimeBasedTriggeringPolicy which would actually sleep upto next rollover instead of the LogRotateThread which sleeps for some indefinite specified time.

Thread rotateThread = new Thread(new LogRotateRunnable(this));
rotateThread.start();


Added the above after initialize(RollingFileManager)

LogRotateRunnable :

while (true) {
        long sleepTime = fTimeBasedTriggeringPolicy.getNextRollover()
                - System.currentTimeMillis();
        if (sleepTime > 0) {
            try {
                Thread.sleep(sleepTime + EMPTY_LOG_EVENT_DELAY);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        fTimeBasedTriggeringPolicy.checkRollover(new EmptyLogEvent());
    }


Also it won't roll empty files, but the good part is of course if atleast one valid log entry within next rollover time, it will.



来源:https://stackoverflow.com/questions/29672459/log4j2-auto-rollover-after-specified-duration

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