logback totalSizeCap 无效 和maxHistory无效 解决
totalSizeCap 无效 升级logbackjar包到1.1.8版本,最好升级到1.2.0版本,因为1.2.0解决了当文件总大小大于2G时不能自动删除旧文件的问题,因为源码中有个文件大小判断是错误的。
源自https://tidyko.com/posts/589711b0.html
maxHistory 无效,需要增加一个设置 <cleanHistoryOnStart>true</cleanHistoryOnStart>,在只有设置为true并且 maxHistory 不为零才能起效。
源自https://www.jianshu.com/p/d9c08785430a
所以修改后的配置应该是
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <!-- rollover daily --> <fileNamePattern>${LOG_HOME}/consumer.%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>50MB</maxFileSize> <maxHistory>30</maxHistory> <totalSizeCap>2GB</totalSizeCap> <cleanHistoryOnStart>true</cleanHistoryOnStart> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> </encoder> </appender>
最后的
<root level="INFO">
<appender-ref ref="FILE" />
</root>
不能少了 ,根据实际配置来,appender name = 后面的参数是啥,就在<appender-ref ref="" /> 写啥
例子:
修改前:
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.home}/ui/${loggingUserId}/mylog_user_%d{yyyy-MM-dd}.log</fileNamePattern> <MaxHistory>7</MaxHistory> </rollingPolicy>
修改后(增加【<cleanHistoryOnStart>true</cleanHistoryOnStart>】这一行):
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.home}/ui/${loggingUserId}/mylog_user_%d{yyyy-MM-dd}.log</fileNamePattern>
<MaxHistory>7</MaxHistory>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>