Moving to SizeAndTimeBasedRollingPolicy from TimeBasedRollingPolicy to rollover every mignight or a max size
Asked Answered
F

1

6

When I use the following appender

<appender name="APPLICATION"
          class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>D:/logs/log.txt</file>
    <append>true</append>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>%p %d [%t] %L - %m%n</Pattern>
    </encoder>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- rollover daily -->
        <fileNamePattern>D:/logs/log.%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>10MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
        <!-- keep 30 days' worth of history -->
        <maxHistory>30</maxHistory>
    </rollingPolicy>
</appender>

I get the following warning:

SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead

So I change the SizeAndTimeBasedFNATP to SizeAndTimeBasedRollingPolicy

which leaves me with this:

<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
        <maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>

but then I get:

No TriggeringPolicy was set for the RollingFileAppender named APPLICATION

Which makes me think that there is no such TriggeringPolicy so I revert the TriggeringPolicy back to SizeAndTimeBasedFNATP and change the rolling policy from TimeBasedRollingPolicy to SizeAndTimeBasedRollingPolicy.

My new rollingPolicy now becomes:

<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
        <!-- rollover daily -->
        <fileNamePattern>D:/logs/log.%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>10MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
        <!-- keep 30 days' worth of history -->
        <maxHistory>30</maxHistory>
</rollingPolicy>

But then I get:

TriggeringPolicy has not started. RollingFileAppender will not start

I am trying to achieve rollover every midnight or after a max size, whichever comes first. Now, I am using logback version: 1.2.3

Fortyfour answered 4/1, 2019 at 5:37 Comment(0)
F
10

finally figured out that maxFileSize had to be outside of timeBasedFileNamingAndTriggeringPolicy and SizeAndTimeBasedRollingPolicy doesn't require this. So, the final logback.xml, which rolls based on time and size (whichever comes first) becomes:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>
    <!-- Send debug messages to System.out -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%p %d [%t] %L - %m%n</pattern>
        </encoder>
    </appender>
    <appender name="APPLICATION" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>D:/logs/log.txt</file>
        <append>true</append>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%p %d [%t] %L - %m%n</Pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!-- rollover daily -->
            <fileNamePattern>D:/logs/log.%d{yyyy-MM-dd HH}.%i.txt</fileNamePattern>
            <maxFileSize>5GB</maxFileSize>
            <!-- keep 30 days' worth of history -->
            <maxHistory>30</maxHistory>
            <totalSizeCap>20GB</totalSizeCap>
        </rollingPolicy>
    </appender>

    <logger name="javaportreader" level="INFO" additivity="false">
        <appender-ref ref="STDOUT" />
    </logger>
    <logger name="application" level="DEBUG" additivity="false">
        <appender-ref ref="APPLICATION"/>
    </logger>

    <!-- By default, the level of the root level is set to DEBUG -->
    <root level="DEBUG">
        <appender-ref ref="STDOUT" />
    </root>
    <root level="INFO">
        <appender-ref ref="APPLICATION"/>
    </root>
</configuration>
Fortyfour answered 4/1, 2019 at 10:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.