什么决定 log4j TimeBasedRollingPolicy 何时滚动?

发布于 2024-10-27 01:25:04 字数 1506 浏览 3 评论 0原文

我正在从 Log4J Extras 设置一个 TimeBasedRollingPolicy,但我不清楚什么告诉策略何时滚动。 API 不是很明确,所以我只是做出推断。听起来好像是 FileNamePattern 中的最后一个元素决定了频率。

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>

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

谢谢!

保罗

I'm setting up a TimeBasedRollingPolicy from Log4J Extras and I am not clear what tells the policy when to roll over. The API is not explicit so I'm just making inferences. It sounds like it's the last element in the FileNamePattern that determines the frequency.

Take this example from the 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-MM means the file should roll when MM changes?

Thanks!

Paul

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为你拒绝所有暧昧 2024-11-03 01:25:04

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

编辑:

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

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).

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.

微暖i 2024-11-03 01:25:04

我得到了这个工作。下面是 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 依赖项:

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

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.

<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:

  <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>apache-log4j-extras</artifactId>
        <version>1.2.17</version>
    </dependency>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文