【发布时间】:2019-04-12 02:45:32
【问题描述】:
我在 log4j2 中使用滚动文件附加程序并使用基于时间的触发策略。这是我的配置文件的样子:
...
<RollingFile name="RollingFile" fileName="logs/temp.log" filePattern="logs/test1-%d{MM-dd-yy-HH-mm-ss-SSS}.log">
<Policies>
<TimeBasedTriggeringPolicy interval="2"></TimeBasedTriggeringPolicy>
</Policies>
<JsonLayout eventEol="true" compact="true"></JsonLayout>
<CustomStrategy />
</RollingFile>
...
我编写了一个扩展DefaultRolloverStrategy 的类CustomStrategy,然后我覆盖了方法rollover,如下所示:
@Override
public RolloverDescription rollover(final RollingFileManager manager) throws SecurityException {
RolloverDescription temp = super.rollover(manager);
//Read file that just got rolled over and do some stuff
return temp;
}
在这种方法中,我需要刚刚翻转的文件的名称,即最初将日志写入temp.log,然后将其翻转到test1-[some timestamp],以读取它并执行某些操作。谁能建议如何获取文件名(test1-[some timestamp])?
【问题讨论】: