Logback removes logs before MaxHistory during hourly rollback

I am using the below appender and I could see the rollback happening at once every hour.

But I mentioned it <maxHistory>

as 10 days. But I could see that the logs are automatically deleted by logback at the end of each day.

But the wait is to keep the log files for no more than 10 days.

<appender name="TIME_BASED_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>c:/logs/timeBasedlogFile.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">        
        <fileNamePattern>c:/logs/timeBasedlogFile.%d{yyyy-MM-dd_HH}.log</fileNamePattern>
        <maxHistory>10</maxHistory>
    </rollingPolicy>
    <encoder>
        <pattern>%relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
</appender> 

      

If I use daily rollover then I could see that expanded logs are maintained for the number of days specified in <maxHistory>

Does maxHistory only work for daily rollover?

+3


source to share


1 answer


The element maxHistory

indicates the number of collapsed files in history.
If you choose <maxHistory>10</maxHistory>

, it means you can have a history for 10 files.

In your case, you specify the hour as the grit to roll. This means that if your application logs itself every hour, history fills up after deleting log logs that are 10 hours or older.

With your configuration files per hour, having exactly 10 days, the story is far from obvious, as your application can be shut down in a few hours or even a few days.

Actually, if you want to keep the hourly granularity, I think you should specify the history in terms of hours, not days: you should align both.



For example, if you think the application is running 12 hours a day, you can specify 120

as a history

value (12 hours * 10 days) to have something close to 10 days.
If you can't keep up with the frequency of the logs, and you want to have no history less than 10 days, use (24 hours * 10 days) 240

as a history

value.
Only downside: if the app doesn't register every hour of every day, you'll get more history as needed.

If I use daily rollover then I could see that collapsed logs are maintained for the number of days specified in <maxHistory>

Yes, because, as explained, the granularity of the value maxHistory

depends on the granularity of the rolling time.

+4


source







All Articles