How do I configure logback to gzip my logs automatically?

Try to do like this, Hope it will work for you.

<appender name="FILE"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <File>${LOGDIR}/filename.log</File>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- rollover daily -->
        <FileNamePattern>${LOGDIR}/file.%d{yyyy-MM-dd}.%i.log.gz
        </FileNamePattern>
        <!-- keep 30 days' worth of history -->
        <MaxHistory>30</MaxHistory>
        <!-- or whenever the file size reaches 10MB -->
        <timeBasedFileNamingAndTriggeringPolicy
            class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>10MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>%date [%thread] %-5level %logger{36} - %msg%n
        </Pattern>
    </encoder>
</appender>

The above code will compress your file on the day basis or If the log file size exceeds 10MB.

Note : I have added “%i” in filePattern, It will iterate your file name as file1,file2 etc.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)