Java 除非有新日志,否则 TimeBasedRollingPolicy 不会滚动

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

TimeBasedRollingPolicy not rolling unless there are new logs

javalogginglogbackrollingfileappender

提问by villager

Here's my config:

这是我的配置:

<appender name="myAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <append>true</append>
    <file>mylogs.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- daily rollover -->
        <fileNamePattern>mylogs-%d{yyyy-MM-dd_HH-mm}.log</fileNamePattern>

        <!-- keep 30 days' worth of history -->
        <maxHistory>30</maxHistory>
    </rollingPolicy>

    <encoder>
        <pattern>%d{HH:mm:ss.SSS} %-5level %logger{36} [%thread] - %M:%L - %msg%n</pattern>
    </encoder>
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>INFO</level>
    </filter>
</appender>

According to the logback's document found here (http://logback.qos.ch/manual/appenders.html#TimeBasedRollingPolicy) , file will be rollover every minute based on my %d{yyyy-MM-dd_HH-mm}fileNamePattern.

根据此处找到的 logback 文档(http://logback.qos.ch/manual/appenders.html#TimeBasedRollingPolicy),文件将根据我的%d{yyyy-MM-dd_HH-mm}fileNamePattern每分钟翻转一次。

I observed how this works and here are my findings:

我观察了这是如何工作的,这是我的发现:

  • It doesn't create a log file very minute.
  • It only create a log file for the previous minute when the a new log arrives. (e.g. I have a log on 11:53pm, and it's now 11:55pm, it doesn't create a new log file immediately for the 11:53pm when it hits 11:54pm, but when a new log came later, say at 11:56pm, it now creates the file for 11:53pm.)
  • 它不会很快创建日志文件。
  • 它仅在新日志到达时创建前一分钟的日志文件。(例如,我在晚上 11:53 有一个日志,现在是晚上 11:55,当它到达晚上 11:54 时,它不会立即为晚上 11:53 创建一个新的日志文件,但是当一个新日志稍后出现时,例如在11:56pm,它现在为 11:53pm 创建文件。)

Am I missing something, I thought it will create a log file every minute?

我是否遗漏了什么,我认为它会每分钟创建一个日志文件?

采纳答案by sheltem

Scroll down further in the section of the documentation you linked and you will find this:

在您链接的文档部分进一步向下滚动,您会发现:

For various technical reasons, rollovers are not clock-driven but depend on the arrival of logging events. For example, on 8th of March 2002, assuming the fileNamePattern is set to yyyy-MM-dd (daily rollover), the arrival of the first event after midnight will trigger a rollover. If there are no logging events during, say 23 minutes and 47 seconds after midnight, then rollover will actually occur at 00:23'47 AM on March 9th and not at 0:00 AM. Thus, depending on the arrival rate of events, rollovers might be triggered with some latency. However, regardless of the delay, the rollover algorithm is known to be correct, in the sense that all logging events generated during a certain period will be output in the correct file delimiting that period.

由于各种技术原因,翻转不是时钟驱动的,而是取决于日志事件的到达。例如,在 2002 年 3 月 8 日,假设 fileNamePattern 设置为 yyyy-MM-dd(每日翻转),午夜后第一个事件的到来将触发翻转。如果在此期间没有记录事件,例如午夜后 23 分 47 秒,则翻转实际上将发生在 3 月 9 日上午 00:23'47 而不是上午 0:00。因此,根据事件的到达率,翻转可能会在一些延迟下触发。但是,无论延迟如何,翻转算法都是正确的,因为在某个时间段内生成的所有日志记录事件都将输出到分隔该时间段的正确文件中。

Short version: It's not time-triggered, but logging-event-triggered. No logging events means no rollover. In a configuration set to rollover each minute that means there will be no files for any minute for which no logging-events arrive.

简短版本:它不是时间触发的,而是日志事件触发的。没有记录事件意味着没有翻转。在设置为每分钟翻转的配置中,这意味着任何分钟都没有记录事件到达的文件。

回答by jake

You don't need input <file> properties.

您不需要输入 <file> 属性。

If you omit that, you can solve your problems

如果你省略它,你可以解决你的问题

Note that the file property in RollingFileAppender (the parent of TimeBasedRollingPolicy) can be either set or omitted. By setting the file property of the containing FileAppender, you can decouple the location of the active log file and the location of the archived log files. The current logs will be always targeted at the file specified by the file property. It follows that the name of the currently active log file will not change over time. However, if you choose to omit the file property, then the active file will be computed anew for each period based on the value of fileNamePattern. The examples below should clarify this point.

请注意,可以设置或省略 RollingFileAppender(TimeBasedRollingPolicy 的父级)中的文件属性。通过设置包含 FileAppender 的 file 属性,您可以将活动日志文件的位置和归档日志文件的位置解耦。当前日志将始终以文件属性指定的文件为目标。因此,当前活动日志文件的名称不会随着时间而改变。但是,如果您选择省略文件属性,则将根据 fileNamePattern 的值为每个周期重新计算活动文件。下面的例子应该澄清这一点。