如何在log4j2中更改滚动附加器(Rolling Appender)的时间间隔?

huangapple go评论81阅读模式
英文:

How to change the rolling appender time interval in log4j2?

问题

我想将滚动记录器从每天更改为每5分钟一次。我想将它更改为一个较低的数字,以便我可以测试它是否正常工作。任何帮助将不胜感激

**代码:**
```plaintext
# RollingFileAppender会将日志打印到文件中,可以基于时间或大小进行轮转
appender.rolling.type = RollingFile
appender.rolling.name = fileLogger
appender.rolling.fileName= C://Users/Kai/dev/Cal-2/app_logs/IT/IT_log.txt
appender.rolling.filePattern= C://Users/kai/dev/Cal-2/app_logs/IT/IT_log_%d{dd-MM-yyyy}.zip
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{dd-MM-yyyy HH:mm:ss.SSS}.-%t-%x-%-5p-%-10c:%m%n
appender.rolling.policies.type = Policies

 

# 每天旋转日志文件并保留14天的日志

appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.delete.type = Delete
appender.rolling.strategy.delete.basePath = C://Users/Kai/dev/Cal-2/app_logs/IT/
appender.rolling.strategy.delete.maxDepth = 1
appender.rolling.strategy.delete.ifLastModified.type = IfLastModified

 

# 删除14天前的文件

appender.rolling.strategy.delete.ifLastModified.age = 14d
英文:

I am looking to change my rolling appender from every day to every 5 minutes. I want to change it to a lower number in order for me to test that it is working. Any help would be appreciated

Code:

# RollingFileAppender will print logs in file which can be rotated based on time or size
appender.rolling.type = RollingFile
appender.rolling.name = fileLogger
appender.rolling.fileName= C://Users/Kai/dev/Cal-2/app_logs/IT/IT_log.txt
appender.rolling.filePattern= C://Users/kai/dev/Cal-2/app_logs/IT/IT_log_%d{dd-MM-yyyy}.zip
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{dd-MM-yyyy HH:mm:ss.SSS}.-%t-%x-%-5p-%-10c:%m%n
appender.rolling.policies.type = Policies

 

# Rotate log file each day and keep 14 days worth

appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.delete.type = Delete
appender.rolling.strategy.delete.basePath = C://Users/Kai/dev/Cal-2/app_logs/IT/
appender.rolling.strategy.delete.maxDepth = 1
appender.rolling.strategy.delete.ifLastModified.type = IfLastModified

 

# Delete files older than 14 days

appender.rolling.strategy.delete.ifLastModified.age = 14d

答案1

得分: 1

正如Usagi在评论中提到的,您需要更改您的filePattern以指定分钟(例如 mm-hh-dd-MM-yyyy)。然后,您应该能够使用CronTriggeringPolicy,其计划为0 */5 * ? * *

如果您向下滚动此页面,您最终会找到关于CronTriggeringPolicy的文档。

英文:

As Usagi mentioned in the comment, you will want to change your filePattern to specify minutes (ex. mm-hh-dd-MM-yyyy). Then you should be able to use a CronTriggeringPolicy with a schedule of 0 */5 * ? * *.

If you scroll down this page pretty far you'll eventually get to the documentation on the CronTriggeringPolicy.

huangapple
  • 本文由 发表于 2020年10月15日 22:19:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64373612.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定