java 什么决定 log4j TimeBasedRollingPolicy 何时翻转?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5434410/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:07:09  来源:igfitidea点击:

What determines when a log4j TimeBasedRollingPolicy rolls over?

javalog4jrollingfileappender

提问by Paul

I'm setting up a TimeBasedRollingPolicyfrom Log4J Extrasand I am not clear what tells the policy when to roll over. The APIis not explicit so I'm just making inferences. It sounds like it's the last element in the FileNamePatternthat determines the frequency.

我正在TimeBasedRollingPolicyLog4J Extras设置一个,我不清楚是什么告诉策略何时翻转。 API不是明确的,所以我只是进行推断。听起来它FileNamePattern是决定频率的最后一个元素。

Take this example from the log4j Wiki:

log4j Wiki 中获取此示例:

<appender name="ROLL" class="org.apache.log4j.rolling.RollingFileAppender">
    <!-- The active file to log to -->
    <param name="file" value="/applogs/myportal/portal.log" />
    <param name="append" value="true" />
    <param name="encoding" value="UTF-8" />

    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
    <!-- The file to roll to, this is a fairly intelligent parameter, if the file
         ends in .gz, it gzips it, based on the date stamp it rolls at that time, 
         default is yyyy-MM-dd, (rolls at midnight)
    -->
        <param name="FileNamePattern" value="/applogs/myportal/portal.%d.log.gz" />
    </rollingPolicy>

    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%5p %d{ISO8601} [%t][%x] %c - %m%n" />
    </layout>
</appender>

Am I to assume that because the pattern ends in dd, the policy is to roll when that changes? Same with an example in the API, a pattern of yyyy-MMmeans the file should roll when MMchanges?

我是否假设因为模式以 结束dd,策略是在发生变化时滚动?与 API 中的示例相同,一种模式yyyy-MM意味着文件在MM更改时应该滚动?

Thanks!

谢谢!

Paul

保罗

采纳答案by Thomas

Well, I'd have to double check, but I'd say whenever the String produced by formatting the current date with the format string changes, the file is rolled. This means: if you format the date using "yyyy-MM-dd" the result would change every day. This would also happen with "dd" only, BUT you'd get the same filename every month, thus the files are either overwritten, be appended to or the rolling fails because the file already exists (not sure which is true, depends on what the appender does, I guess in this case logs will be appended, except maybe for the gzip way).

好吧,我必须仔细检查,但我会说每当通过使用格式字符串格式化当前日期产生的字符串发生变化时,文件就会被滚动。这意味着:如果您使用“yyyy-MM-dd”格式化日期,则结果每天都会改变。这也只会发生在 "dd" 上,但是你每个月都会得到相同的文件名,因此文件要么被覆盖,要么被附加,要么滚动失败,因为文件已经存在(不确定哪个是真的,取决于什么附加程序确实如此,我想在这种情况下将附加日志,除了 gzip 方式)。

Edit:

编辑:

Example: if you have mylog.%d{dd}.log, the resulting log file for today (2011-03-27) have the name mylog.25.log(due to formatting new Date() when logging) and would append messages to that file. Tomorrow, the now used file would have the name mylog.26.log. In April 25th you'd again get filename `mylog.25.log thus all logs from that day would be appended to the file which already contains logs from March 25th.

示例:如果您有mylog.%d{dd}.log,则今天 (2011-03-27) 的结果日志文件具有名称mylog.25.log(由于在记录时格式化 new Date())并将消息附加到该文件。明天,现在使用的文件将具有名称mylog.26.log. 在 4 月 25 日,您将再次获得文件名 `mylog.25.log,因此当天的所有日志都将附加到已包含 3 月 25 日日志的文件中。

回答by Prateek Sharma

I got this working. Below is the code for log4j. You only need to add log4j-extras dependencies in pom.xml and use below code: The below code rolls at every minutes and creates .zip file.

我得到了这个工作。下面是 log4j 的代码。您只需要在 pom.xml 中添加 log4j-extras 依赖项并使用以下代码: 下面的代码每分钟滚动一次并创建 .zip 文件。

<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">    
    <param name="FileNamePattern" value="/opt/app/srdotcom/logs/portal.%d{yyyy-MM-dd-HH-mm}.log.zip" />
</rollingPolicy>

<layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d{YYYY-MM-dd HH:mm:ss:SSS z}| %c{2}| %m%n" />
</layout>

Maven Dependencies:

Maven 依赖项:

  <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>apache-log4j-extras</artifactId>
        <version>1.2.17</version>
    </dependency>