How to limit the number of files per day when using Logback SizeAndTimeBasedFNATP

扶醉桌前 提交于 2021-02-08 17:25:57

问题


When using the Logback SizeAndTimeBasedFNATP triggering policy, how can the number of files per day be limited? For example, on any given day, I don't want to have more than 100MB of logs. Given that each log (in the example below) is 20MB, I would want to be able to set a max limit of 5 files per day.

The FixedWindowRollingPolicy provides a maxIndex property, but the TimeBasedRollingPolicy does not have maxIndex. Is there a recommended approach to applying a maxIndex when using the TimeBasedRollingPolicy?

<appender name="some.file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    <fileNamePattern>logs/some_app_%d{yyyyMMdd}.log.%i</fileNamePattern>
    <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
        <maxFileSize>20MB</maxFileSize>
    </timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
    <pattern>%level %date{yyyy-MM-dd HH:mm:ss:SSS} %msg%n</pattern>
</encoder>


回答1:


currently this is not possible. Look at this answer Logback, set max history files per day. You cannot rollover both time and size based rolling/triggering policy.




回答2:


It's possible to limit total size of logs with ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy

<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
    <fileNamePattern>log/log.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
    <!-- each file should be at most 100MB, keep 60 days worth of history, but at most 20GB -->
    <maxFileSize>100MB</maxFileSize>
    <maxHistory>60</maxHistory>
    <totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>

From logback docs



来源:https://stackoverflow.com/questions/9101627/how-to-limit-the-number-of-files-per-day-when-using-logback-sizeandtimebasedfnat

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!