我想通过以下方式更改 ElasticSearch 的 log4j2 配置。来自 ElasticSearch 的日志应该保存在目录中:/path/to/log/{year}/{month}/{day}/cluster_name.log
但 TimeBasedTriggeringPolicy 仅在一天结束后才会进行滚动更新。我一直在尝试使用基于时间的滚动策略,但无法通过 *.properties 文件进行配置。我将整个 log4j2.properties 重写为 log4j2.xml 文件,但 ElasticSearch 需要 log4j2.properites。最后,我决定辞职,不再将信件日的日志记录到适当的目录。我返回到基于时间的触发策略,我使用了这个文件模式/path/to/log/%d{yyyy/MM/dd}/cluster_name.log
但仍然不起作用。
配置文件的大部分:
appender.rolling.type = RollingFile
appender.rolling.name = rolling
appender.rolling.fileName = /path/to/log/cluster_name.log
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = [%d{ISO8601}][%-5p][%-25c{1.}] %marker%.-10000m%n
appender.rolling.filePattern = /path/to/log/%d{yyyy/MM/dd}/cluster_name.log
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
我认为%d{yyyy/MM/dd}
模式将创建名称为2017/09/19
的目录,这是无效的目录名称。这就是为什么,它不起作用。
试试下面的filePattern
-
appender.rolling.filePattern = /path/to/log/$${date:yyyy}/$${date:MM}/$${date:dd}/cluster_name_%d{yyyy-MM-dd}.log
它将轮换日志文件,如下所示 -
/path/to/log/{year}/{month}/{day}/cluster_name_{date}.log
文件名中的日期是强制性的。没有这个,它可能不起作用。