1. 程式人生 > 實用技巧 >llogback totalSizeCap 無效 和maxHistory無效 解決

llogback 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>