Rolling logs by both size and time

后端 未结 3 1193
粉色の甜心
粉色の甜心 2020-12-16 21:06

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<

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-16 21:28

    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.

提交回复
热议问题