log4j2滚动文件名

时间:2015-09-28 08:39:07

标签: java log4j2 rollingfileappender

有没有办法获得滚动文件名?我编写了自定义插件来指示文件滚动,但我需要获取滚动文件的文件名。我的插件从java代码发出信号:

自定义插件:

@Plugin(name = "RTriggerPolicy", category = "Core", printObject = true)
public class RTriggerPolicy implements TriggeringPolicy {
    //...
    @Override
    public boolean isTriggeringEvent(LogEvent event) {
        if (event instanceof RLogEvent)
            return true;
        return false;
    }
    //...
}

log4j2.xml:

<RollingFile name="appFile" fileName="${dir}app.log" append="true" filePattern="${dir}${roll}app-%d{yyyyMMddHHmmssSSS}.log">
    <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY" />
    <PatternLayout pattern="${pattern}" />
    <Policies>
        <RTriggerPolicy name="app"/>
    </Policies>
</RollingFile>

1 个答案:

答案 0 :(得分:1)

获取滚动文件名的唯一方法是扩展DefaultRolloverStrategy并覆盖rollover方法。然后,您将其实现为:

@Override
public RolloverDescription rollover(final RollingFileManager manager) throws SecurityException {
    RolloverDescription rolloverDescription = super.rollover(manager);
    if (rolloverDescription.getSynchronous instanceof FileRenameAction) {
        File file = ((FileRenameAction) rolloverDescription.getSynchronous()).getDestination();
        // Do something useful here.
    }
}