Reputation: 659
Any help or hint would be greatly appreciated!!
My SpringBoot application is logging only 1 day. I want it to log more than 30 days.
logback-spring.xml
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<charset>UTF-8</charset>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<file>${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${LOG_FILE}.%i</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
<logger name="ca.test.hub" level="INFO" additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</logger>
<logger name="org.apache.cxf" level="INFO" additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</logger>
<logger name="org.hibernate" level="DEBUG" additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</logger>
<logger name="org.springframework" level="INFO" additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</logger>
application.yml:
logging:
file: logs/test.log pattern: console: "%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M - %msg%n" file: "%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M - %msg%n" level: root: INFO
Upvotes: 0
Views: 1696
Reputation: 2821
If you want to keep log files for more than 30 days your best bet is not to use FixedWindowRollingPolicy
(documentation), which is used if you only want to limit the amount of log files that are kept on the file system.
A better appender suited for your requirements would be SizeAndTimeBasedRollingPolicy
which, as stated in the documentation,
archive[s] files essentially by date but at the same time limit the size of each log file
My suggested edit to your current FILE
appender would be something like this
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<charset>UTF-8</charset>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE}-%d{yyyy-MM-dd}.%i</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
This SizeAndTimeBasedRollingPolicy
stores files with the fileNamePattern
LOG_FILE-2019-05-02.0
(LOG_FILE would of course be the value for the ${LOG_FILE}
variable). This file will roll over every time it reaches a size larger that 10MB - and then a new file, LOG_FILE-2019-05-02.1
, will be created and written to.
First log to be written after midnight would then create a new file, LOG_FILE-2019-05-03.0
, and write to it. Log files older than 30 days will be deleted. Adjust the configuration to best suit your needs.
I'd recommend you read the documentation for this appender to get a bit more details.
Upvotes: 0