Log4j2 Rolling appender - fileName根据模式“滑动”

时间:2020-12-20 21:50:44

I am looking for rollover strategy where current log (active output target in manual's terminology) file name is not fixed but specified by a pattern, or - more precisely - same pattern as in filePattern attribute.

我正在寻找翻转策略,其中当前日志(手动术语中的活动输出目标)文件名不是固定的,而是由模式指定,或者更准确地说 - 与filePattern属性中的模式相同。

I want to achieve daily rollover where today's log is, say, log-2015-05-05.log and on midnight framework just stops writing it and starts writing into log-2015-05-06.log. However, AFAIK, current configuration allows only

我想实现每日翻转,其中今天的日志是,例如,log-2015-05-05.log,并且在午夜框架上停止编写它并开始写入log-2015-05-06.log。但是,AFAIK,目前的配置只允许

<RollingFile name="ROLFILE"
    fileName="log.log"
    filePattern="log-%d{yyyy-MM-dd}.log"
>

Specifying same value into fileName attribute doesn't work (leads to file with sensitive characters literally interpreted). I noticed no example or SO question with such a dynamic value of fileName. Note the fileName="log-${date:yyyy-MM-dd}.log" doesn't solve problem since expression is evaluated only at startup and events are still sent into file even if their timestamp doesn't match the expression.

在fileName属性中指定相同的值不起作用(导致具有字面解释的敏感字符的文件)。我注意到没有带有fileName动态值的示例或SO问题。请注意,fileName =“log - $ {date:yyyy-MM-dd} .log”不能解决问题,因为只有在启动时才会计算表达式,即使它们的时间戳与表达式不匹配,事件仍然会被发送到文件中。

I am migrating from Log4j 1.2 to Log4j 2.2. In old version, required behavior was possible using

我正在从Log4j 1.2迁移到Log4j 2.2。在旧版本中,可以使用所需的行为

<appender name="ROLFILE" class="org.apache.log4j.rolling.RollingFileAppender">
    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
        <param name="FileNamePattern" value="log-%d{yyyy-MM-dd}.log" />
    </rollingPolicy>
    ...

I prefer to preserve current way since some log analyzing tools rely on it. Is it possible in Log4j2? Thanks.

我更喜欢保留当前的方式,因为一些日志分析工具依赖它。在Log4j2中可以吗?谢谢。

4 个解决方案

#1


0  

Note sure if this works, but you can try using a double $$ in the date lookup. This allows the variable to be resolved at runtime.

请注意这是否有效,但您可以尝试在日期查找中使用double $$。这允许在运行时解析变量。

<RollingFile name="ROLFILE"
    fileName="log-$${date:yyyy-MM-dd}.log"
    filePattern="oldlog-%d{yyyy-MM-dd}.log"
>

You may need to be careful to ensure that the active output target file name is different from the after-rollover file name. (I used 'oldlog' in the snippet above.)

您可能需要小心确保活动输出目标文件名与翻转后文件名不同。 (我在上面的代码段中使用了'oldlog'。)

#2


0  

Finally, I wrote my own rollover strategy which generates different set of rollover actions. Instead renaming active file the active file name is simply replaced inside RollingFileManager. Yes, it's ugly reflection hack and also appender must be initialized with fileName constant corresponding with current date and having same pattern, e.g.

最后,我编写了自己的翻转策略,生成不同的翻转操作集。而是重命名活动文件,只需在RollingFileManager中替换活动文件名。是的,它是丑陋的反射黑客,并且还必须使用与当前日期相对应且具有相同模式的fileName常量来初始化appender,例如,

<RollingFile name="ROLFILE"
    fileName="log-${date:yyyy-MM-dd}.log"
    filePattern="log-%d{yyyy-MM-dd}.log"
>
    <SlidingFilenameRolloverStrategy />
    ...

yet for me this solution is worth doing it despite these small drawbacks. (Initial fileName stays forever as a key in AbstractManager registry MAP even if in manager itself it has been changed - seems it doesn't mind, I also tried replacing manager in registry for new one but it's impossible to collect all parameters necessary for its construction.)

但对我来说,这个解决方案值得这样做,尽管有这些小缺点。 (初始fileName永远保留为AbstractManager注册表MAP中的一个键,即使在管理器本身已被更改 - 似乎不介意,我也尝试在注册表中替换管理器中的新管理器但是不可能收集构建它所需的所有参数。)

I hope this hack shouldn't have been so ugly if RollingFileManager API made it possible normal way. I got some hope seeing this javadoc but framework AFAIK never utilizes this field, let alone for mutating RollingFileAppender.

如果RollingFileManager API使正常的方式成为可能,我希望这个hack不应该如此丑陋。我有一些希望看到这个javadoc但是框架AFAIK从不使用这个字段,更不用说改变RollingFileAppender了。

#3


0  

I think it would work just fine using:

我认为它可以正常使用:

 fileName="log-${date:yyyy-MM-dd}.log"
 filePattern="log-%d{yyyy-MM-dd}.log"

I use it with log4j2 version 2.5

我用它与log4j2版本2.5

#4


-1  

This has been implemented in Log4j 2.8 (see issue LOG4J2-1101).

这已经在Log4j 2.8中实现(参见问题LOG4J2-1101)。

Currently it only works with a RollingFile appender by omitting the filename parameter and using a DirectWriteRolloverStrategy.

目前它只适用于RollingFile appender,省略了filename参数并使用了DirectWriteRolloverStrategy。

Also, this feature seems to have some issues with the TimeBasedTriggeringPolicy (the first rollover doesn't happen so every logfile is offset by one interval); CronTriggeringPolicy works properly.

此外,此功能似乎与TimeBasedTriggeringPolicy存在一些问题(第一次翻转不会发生,因此每个日志文件都会偏移一个间隔); CronTriggeringPolicy正常工作。

Example config:

<RollingRandomAccessFile name="MyLogger"
    filePattern="logs/application.%d{yyyy-MM-dd}.log">
    <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    <Policies>
        <CronTriggeringPolicy schedule="0 * * * * ?" evaluateOnStartup="true"/>
    </Policies>
    <DirectWriteRolloverStrategy/>
</RollingRandomAccessFile>

Support for RollingRandomAccessFile appender is requested in issue LOG4J2-1878.

问题LOG4J2-1878中请求支持RollingRandomAccessFile appender。

Edit: Changed to CronTriggeringPolicy policy after finding TimeBasedTriggeringPolicy had issues.

编辑:找到TimeBasedTriggeringPolicy时遇到问题,更改为CronTriggeringPolicy策略。

#1


0  

Note sure if this works, but you can try using a double $$ in the date lookup. This allows the variable to be resolved at runtime.

请注意这是否有效,但您可以尝试在日期查找中使用double $$。这允许在运行时解析变量。

<RollingFile name="ROLFILE"
    fileName="log-$${date:yyyy-MM-dd}.log"
    filePattern="oldlog-%d{yyyy-MM-dd}.log"
>

You may need to be careful to ensure that the active output target file name is different from the after-rollover file name. (I used 'oldlog' in the snippet above.)

您可能需要小心确保活动输出目标文件名与翻转后文件名不同。 (我在上面的代码段中使用了'oldlog'。)

#2


0  

Finally, I wrote my own rollover strategy which generates different set of rollover actions. Instead renaming active file the active file name is simply replaced inside RollingFileManager. Yes, it's ugly reflection hack and also appender must be initialized with fileName constant corresponding with current date and having same pattern, e.g.

最后,我编写了自己的翻转策略,生成不同的翻转操作集。而是重命名活动文件,只需在RollingFileManager中替换活动文件名。是的,它是丑陋的反射黑客,并且还必须使用与当前日期相对应且具有相同模式的fileName常量来初始化appender,例如,

<RollingFile name="ROLFILE"
    fileName="log-${date:yyyy-MM-dd}.log"
    filePattern="log-%d{yyyy-MM-dd}.log"
>
    <SlidingFilenameRolloverStrategy />
    ...

yet for me this solution is worth doing it despite these small drawbacks. (Initial fileName stays forever as a key in AbstractManager registry MAP even if in manager itself it has been changed - seems it doesn't mind, I also tried replacing manager in registry for new one but it's impossible to collect all parameters necessary for its construction.)

但对我来说,这个解决方案值得这样做,尽管有这些小缺点。 (初始fileName永远保留为AbstractManager注册表MAP中的一个键,即使在管理器本身已被更改 - 似乎不介意,我也尝试在注册表中替换管理器中的新管理器但是不可能收集构建它所需的所有参数。)

I hope this hack shouldn't have been so ugly if RollingFileManager API made it possible normal way. I got some hope seeing this javadoc but framework AFAIK never utilizes this field, let alone for mutating RollingFileAppender.

如果RollingFileManager API使正常的方式成为可能,我希望这个hack不应该如此丑陋。我有一些希望看到这个javadoc但是框架AFAIK从不使用这个字段,更不用说改变RollingFileAppender了。

#3


0  

I think it would work just fine using:

我认为它可以正常使用:

 fileName="log-${date:yyyy-MM-dd}.log"
 filePattern="log-%d{yyyy-MM-dd}.log"

I use it with log4j2 version 2.5

我用它与log4j2版本2.5

#4


-1  

This has been implemented in Log4j 2.8 (see issue LOG4J2-1101).

这已经在Log4j 2.8中实现(参见问题LOG4J2-1101)。

Currently it only works with a RollingFile appender by omitting the filename parameter and using a DirectWriteRolloverStrategy.

目前它只适用于RollingFile appender,省略了filename参数并使用了DirectWriteRolloverStrategy。

Also, this feature seems to have some issues with the TimeBasedTriggeringPolicy (the first rollover doesn't happen so every logfile is offset by one interval); CronTriggeringPolicy works properly.

此外,此功能似乎与TimeBasedTriggeringPolicy存在一些问题(第一次翻转不会发生,因此每个日志文件都会偏移一个间隔); CronTriggeringPolicy正常工作。

Example config:

<RollingRandomAccessFile name="MyLogger"
    filePattern="logs/application.%d{yyyy-MM-dd}.log">
    <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    <Policies>
        <CronTriggeringPolicy schedule="0 * * * * ?" evaluateOnStartup="true"/>
    </Policies>
    <DirectWriteRolloverStrategy/>
</RollingRandomAccessFile>

Support for RollingRandomAccessFile appender is requested in issue LOG4J2-1878.

问题LOG4J2-1878中请求支持RollingRandomAccessFile appender。

Edit: Changed to CronTriggeringPolicy policy after finding TimeBasedTriggeringPolicy had issues.

编辑:找到TimeBasedTriggeringPolicy时遇到问题,更改为CronTriggeringPolicy策略。