java Log4j2自动滚动调度程序
如何为每30秒的翻滚配置log4j2
例如,如果今天是2019-01-09 09:45:00,我通过命令将数据写入日志文件
log.info("test")
10分钟内没有更多数据进入日志, 但我需要2019-01-09 09:45:30的30秒后自动翻转日志文件
如果我这样配置log4j2:
<RollingFile name="RollingFile">
<FileName>C:/log/mylog.log</FileName>
<FilePattern>C:/log/time-based-logs/%d{yyyy-MM-dd-hh-mm}.log.zip</FilePattern>
<PatternLayout>
<Pattern>%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="2" modulate="true" />
</Policies>
<DefaultRolloverStrategy max="5" />
</RollingFile>
我必须通过命令写入日志数据
log.info("test1") // 2019-01-09 09:55:00
然后在10分钟后滚动文件,但我需要每30秒,甚至不再将数据写入日志
我怎么做
我为我糟糕的英语道歉
# 1 楼答案
我想到了这个选择
# 2 楼答案
使用RollingFileAppender https://logback.qos.ch/manual/appenders.html#RollingFileAppender
和基于时间的政策https://logback.qos.ch/manual/appenders.html#TimeBasedRollingPolicy
但这允许你将颗粒细化到每分钟(和每秒钟,但不是30秒)
如果这还不够,请编写自己的自定义RollingPolicy https://logback.qos.ch/apidocs/ch/qos/logback/core/rolling/RollingPolicy.html
或者只是扩展TimeBasedRollingPolicy以包含其他功能 https://logback.qos.ch/apidocs/ch/qos/logback/core/rolling/TimeBasedRollingPolicy.html