本文介绍了log4j2中基于时间的触发策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试每小时创建新的日志文件。我在RollingFileAppender中使用lo4j2的TimeBasedTriggerringPolicy。下面是我从log4j2官方站点获取的示例xml配置代码。

I am trying to create new log files on an hourly basis. I am using TimeBasedTriggerringPolicy of lo4j2 in RollingFileAppender. Below is the sample xml configuration code i have taken from log4j2 official site.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
   <Appenders>
      <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/$${date:yyyy-MM}/app-%d{yyyy-MM-dd-HH}-%i.log.gz">
         <PatternLayout>
            <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
         </PatternLayout>
         <Policies>
            **
            <TimeBasedTriggeringPolicy interval="1" modulate="true" />
            **
            <SizeBasedTriggeringPolicy size="250 MB" />
         </Policies>
      </RollingFile>
   </Appenders>
   <Loggers>
      <Root level="error">
         <AppenderRef ref="RollingFile" />
      </Root>
   </Loggers>
</Configuration>

在区间属性中,我设置了1表示1小时。
但我的文件仍然不会每1小时滚动一次。

In the interval attribute I have set 1 which signifies 1 hour.But still my file does not roll every 1 hour.

请帮我找错。

注意:我已经包含了log4j2的beta9(这是最新的)

Note : I have included beta9 of log4j2 (which is the latest)

推荐答案


1这里表示1天而不是1小时。我已使用以下配置手动测试。

1 here indicates 1 day and not 1 hour. I have manually tested with below configuration.

<RollingFile name="T" fileName="/data_test/log/abc.log"
        filePattern="/data_test/log/abc-%d{MM-dd-yyyy}-%i.log">
        <PatternLayout>
            <Pattern>%d{ISO8601} %-5p [%t] (%F:%L) - %m%n</Pattern>
        </PatternLayout>
        <Policies>              
            <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            <SizeBasedTriggeringPolicy size="100 KB" />
        </Policies>
    </RollingFile>

对于手动测试,我更改了系统日期和时间。
首先,尝试增加1小时。将生成日志文件,但不会按预期生成。
然后更改系统日期,增加1天,然后查看结果。

For manual testing, I change the system date and time.First, try with increasing 1 hour. The log files will be generated but not as per the expectation.Then change the system date, increase by 1 day and then see the results.

假设第29天至10月的最后一个日志文件(abc.log)是50 KB。配置大小为100 KB。如果我们改变一天(增加1天)然后运行。
然后,最后一个文件将被重命名为29-Oct-(某个序列号).log(复制时为50 KB文件),并使用abc.log创建新文件

Suppose the last log file (abc.log) on day 29-Oct is of 50 KB. Configuration size is 100 KB. If we change the day (increase by 1 day) and then run.Then, last file will be renamed 29-Oct-(some sequence number).log (50 KB file as it is copied) and new file will be created with abc.log

我在web.xml中使用以下配置的简单servlet尝试过此操作

I have tried this with simple servlet with below configuration in web.xml

<context-param>
    <param-name>log4jConfiguration</param-name>
    <param-value>log4j2.xml</param-value>
</context-param>

将log4j2.xml保存在src文件夹中。如果我们将它保存在类路径中,则不会加载log4j2.xml。

keep log4j2.xml in src folder. log4j2.xml is not loaded if we keep it in classpath.

这篇关于log4j2中基于时间的触发策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 04:28