Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

logback deletes the logs before the MaxHistory during hourly rollback

I use the below appender and I could see the roll back occurs at once in every hour.

But I mentioned the <maxHistory> as 10 days. But I could see the logs are automatically deleted by logback at the end of every day.

But the expectation is to keep the log files for maximum 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 the the rolled over logs are maintained for the number of days mentioned in <maxHistory>

Does the maxHistory work only for daily rollover?

like image 415
Alagammal P Avatar asked Mar 24 '17 11:03

Alagammal P


1 Answers

The maxHistory element specifies the number of rolled files to history.
If you choose <maxHistory>10</maxHistory> it means you could have a history for 10 files.

In your case you specify the hour as granularity for the rolling. It means if your application logs each hour, the history is full after 10 hours and older history logs are purged.

With your hour rolling file configuration, having exactly 10 days as history is far from being obvious as your application could be off some hours and even some days.

In fact, if you want to keep the hour rolling granularity I think you should specify the history in terms of hours and not days : you have to align both.

For example, if you consider that the application is on 12 hours on day, you could specify 120 as history value (12 hours * 10 days) to have something close to 10 days.
If you don't master the frequency of logs and you want ensure that you have not a history with less than 10 days, use 240 as history value (24 hours * 10 days).
The single drawback : if the application doesn't log each hour of each day, you will get more history as needed.

If I use daily rollover then I could see the the rolled over logs are maintained for the number of days mentioned in <maxHistory>

Yes because as explained, the granularity of the maxHistory value depends on the rolling time granularity.

like image 64
davidxxx Avatar answered Nov 14 '22 22:11

davidxxx