'How to make my logback.xml zip all my logs
Is there a way I can have my logback file zip all my logs after a week on spring.
Solution 1:[1]
Yes, You can use RollingFileAppender. As RollingFileAppender has TimeBasedRollingPolicy, So as soon as that policy is triggered you can tell logback to create the zip, while as per the documentation they say that Logback TimeBasedRollingPolicy/ SizeAndTimeBasedRollingPolicy has automatically zip compression which is Synchronous in nature.
Please check below code snippet.
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_FILE_NAME}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE_NAME}-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>30</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
As you can see, SizeAndTimeBasedRollingPolicy is being used and fileNamePattern ends with .gz compression.
You can similarly use TimeBasedPolicy also.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Gaurav Gupta |