logback的日志輸出配置如下
<!-- 日志文件輸出 -->
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${log.base}/${log.moduleName}.log
</File><!-- 設置日志不超過${log.max.size}時的保存路徑,注意如果 是web項目會保存到Tomcat的bin目錄 下 -->
<!-- 滾動記錄文件,先將日志記錄到指定文件,當符合某個條件時,將日志記錄到其他文件。-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${log.base}/archive/${log.moduleName}_all_%d{yyyy-MM-dd}.%i.log.zip
</FileNamePattern>
<!-- 當天的日志大小 超過${log.max.size}時,壓縮日志并保存 -->
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${log.max.size}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>${log.max.history}</maxHistory>
</rollingPolicy>
<!-- 日志輸出的文件的格式 -->
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread]%logger{56}.%method\(\):%L -%msg%n</pattern>
</layout>
</appender>
其中如下定義了壓縮和歷史日志的保存策略,有兩個比較重要的參數(shù):maxFileSize硅确,maxHistory
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${log.max.size}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>${log.max.history}</maxHistory>
先看一下繼承關系圖
maxHistory默認為0循未,由源碼可以看出(TimeBasedRollingPolicy-start())嫂粟,只有不為0并且清理任務開啟標志位true時會觸發(fā)清理操作
if (maxHistory != INFINITE_HISTORY) {
archiveRemover = timeBasedFileNamingAndTriggeringPolicy.getArchiveRemover();
archiveRemover.setMaxHistory(maxHistory);
if(cleanHistoryOnStart) {
addInfo("Cleaning on start up");
archiveRemover.clean(new Date(timeBasedFileNamingAndTriggeringPolicy.getCurrentTime()));
}
}
清理過程會先計算需要清理的時間范圍
int computeElapsedPeriodsSinceLastClean(long nowInMillis) {
long periodsElapsed = 0;
//如果尚未執(zhí)行過清理操作,則默認清理除保留天數(shù)外64天以內的日志(由INACTIVITY_TOLERANCE_IN_MILLIS決定)
if (lastHeartBeat == UNINITIALIZED) {
addInfo("first clean up after appender initialization");
periodsElapsed = rc.periodsElapsed(nowInMillis, nowInMillis + INACTIVITY_TOLERANCE_IN_MILLIS);
if (periodsElapsed > MAX_VALUE_FOR_INACTIVITY_PERIODS)
periodsElapsed = MAX_VALUE_FOR_INACTIVITY_PERIODS;
} else {
//如果已經(jīng)執(zhí)行過清理操作嫡良,則清理從上次到當前時間的需要清理的時間周期。
periodsElapsed = rc.periodsElapsed(lastHeartBeat, nowInMillis);
if (periodsElapsed < 1) {
addWarn("Unexpected periodsElapsed value " + periodsElapsed);
periodsElapsed = 1;
}
}
return (int) periodsElapsed;
}
接著就會按天來清理
public void clean(Date now) {
long nowInMillis = now.getTime();
int periodsElapsed = computeElapsedPeriodsSinceLastClean(nowInMillis);
lastHeartBeat = nowInMillis;
if (periodsElapsed > 1) {
addInfo("periodsElapsed = " + periodsElapsed);
}
//按天清理
for (int i = 0; i < periodsElapsed; i++) {
//periodOffsetForDeletionTarget=-maxHistory-1蹂楣,如maxHistory為2犯祠,則該值為-3
cleanByPeriodOffset(now, periodOffsetForDeletionTarget - i);
}
}
刪除時會根據(jù)當天時間月劈,生成一個正則表達式:/*****/rsms_all_2018-04-09.(\d{1,3}).log.zip而姐,滿足條件的文件就會被清理掉政鼠。
由以上可以得出兩個結論
- 如果首次項目啟動時胡桨,超出maxHistory定義的時間的64天之前的日志是不會被清理的
-
如果當天日志的編號超出3位數(shù)后綴,也將不會被清理