Java log4j2 中基于时间的触发策略
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19304165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Time based triggering policy in log4j2
提问by user1890780
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.
我正在尝试每小时创建一次新的日志文件。我在 RollingFileAppender 中使用 lo4j2 的 TimeBasedTriggerringPolicy。下面是我从 log4j2 官方网站上获取的示例 xml 配置代码。
<?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>
In the interval attribute I have set 1 which signifies 1 hour. But still my file does not roll every 1 hour.
在间隔属性中,我设置了 1,表示 1 小时。但是我的文件仍然不是每 1 小时滚动一次。
Please help me find any mistake.
请帮我找出任何错误。
Note : I have included beta9 of log4j2 (which is the latest)
注意:我已经包含了 log4j2 的 beta9(这是最新的)
采纳答案by Zubin Shah
1 here indicates 1 day and not 1 hour. I have manually tested with below configuration.
这里的 1 表示 1 天而不是 1 小时。我已经使用以下配置进行了手动测试。
<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>
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.
对于手动测试,我更改了系统日期和时间。首先,尝试增加 1 小时。将生成日志文件,但不会按照预期生成。然后更改系统日期,增加1天然后查看结果。
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
假设第 29 天至 10 月的最后一个日志文件 (abc.log) 为 50 KB。配置大小为 100 KB。如果我们更改日期(增加 1 天)然后运行。然后,最后一个文件将重命名为 29-Oct-(某个序列号).log(复制时为 50 KB 文件),并且将使用 abc.log 创建新文件
I have tried this with simple servlet with below configuration in web.xml
我已经在 web.xml 中使用以下配置的简单 servlet 进行了尝试
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>log4j2.xml</param-value>
</context-param>
keep log4j2.xml in src folder. log4j2.xml is not loaded if we keep it in classpath.
将 log4j2.xml 保存在 src 文件夹中。如果我们将它保存在类路径中,则不会加载 log4j2.xml。
回答by Remko Popma
You do have a non-empty log file (otherwise there is nothing to roll over)?
你有一个非空的日志文件(否则没有什么可以翻转的)?
Note that even though the name is "TimeBased..." It will not actually roll over at the specified time, but at the first log event that arrives after the time threshold has been exceeded. Can you try with a small test program that logs something after 61 minutes or so and see if the problem still occurs?
请注意,即使名称是“TimeBased...”,它实际上不会在指定时间翻转,而是在超过时间阈值后到达的第一个日志事件上翻转。您可以尝试使用一个在 61 分钟左右后记录一些内容的小型测试程序,看看问题是否仍然存在?
If it doesn't roll over with the above test program, you may have found a bug. In that case please raise it on the log4j issue tracker. (Be sure to attach the test program the team can use to reproduce the issue).
如果上面的测试程序没有翻车,你可能已经发现了一个bug。在这种情况下,请在 log4j 问题跟踪器上提出它。(请务必附上团队可以用来重现问题的测试程序)。
回答by rajnish
As Abid mentioned, interval value is interpreted in context of pattern that is specified as part of filePattern. It starts with lowest denomination. For example,if pattern contains S, frequency will be in millisecond. It supports the date pattern as described in detail as part of SimpleDateFormat java doc http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
正如 Abid 所提到的,间隔值是在指定为 filePattern 一部分的模式的上下文中解释的。它从最低面额开始。例如,如果模式包含 S,频率将以毫秒为单位。它支持作为 SimpleDateFormat java doc http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html 的一部分详细描述的日期模式
回答by Amin Sh
Log4j documentations:
Log4j文档:
interval -> (integer) How often a rollover should occur based on the most specific time unit in the date pattern. For example, with a date pattern with hours as the most specific item and and increment of 4 rollovers would occur every 4 hours. The default value is 1.
interval -> (integer) 根据日期模式中最具体的时间单位,翻转应该发生的频率。例如,日期模式以小时为最具体的项目,并且每 4 小时会发生 4 次翻转。默认值为 1。
You should change the filename pattern if you would like to create it every hour.
如果您想每小时创建一次,您应该更改文件名模式。
回答by Akshay Kumar
According to your TimeBasedTriggeringPolicy configuration, logger will only populate the logs every day not every hour. AFAIK,You can achieve the functionality by changing the filePattern from HH(Hours) to dd(Days).
根据您的 TimeBasedTriggeringPolicy 配置,logger 只会每天而不是每小时填充日志。AFAIK,您可以通过将 filePattern 从 HH(Hours) 更改为 dd(Days) 来实现该功能。
I have modified your config.xml. Try This
我已经修改了你的 config.xml。尝试这个
<?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}-%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>
For more details check this
有关更多详细信息,请检查此
Hope this will workout for you too.
希望这也能锻炼你。