TimeBasedRollingPolicy not rolling unless there are new logs
Asked Answered
F

2

16

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 TimeBasedRollingPolicy , file will be rollover every minute based on my %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.)

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

Felipa answered 5/3, 2014 at 4:58 Comment(0)
A
24

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.

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.

Antoine answered 5/3, 2014 at 8:22 Comment(4)
That's informative. Thank you for that. Would there be a way/config to have it time-triggered not logging-event-triggered?Felipa
To my knowledge, there is no way to configure logback to create empty log files (that is what would happen). Rolling always happens on the next logging event after the cutoff time/size. A workaround might be possible with a scheduler sending a dummy log-event at the start of every minute to force creation of a new log file without a true log event, but I wouldn't like such behaviour in one of my applications. Why do you want empty log files any way? If there were no logging events for a few minutes I wouldn't expect log files for that time either.Antoine
No, I do not want empty log files, too. The behavior that I'm after is for log files for the day to be created exactly at midnight, not when a new log file arrived.Felipa
In logback that is only possible by having an event arrive exactly at midnight. If it's vitally important, you could try the dummy-event approach.Antoine
P
5

You don't need input <file> properties.

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.

Piteous answered 26/2, 2015 at 8:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.