Note that the file property in RollingFileAppender (the parent of TimeBasedRollingPolicy) can be either set or omitted. By setting the file property of the containing FileAppender, you can decouple the location of the active log file and the location of the archived log files. The current logs will be always targeted at the file specified by the file property. It follows that the name of the currently active log file will not change over time. However, if you choose to omit the file property, then the active file will be computed anew for each period based on the value of fileNamePattern.bash
FileAppender
包含的屬性file
,系統實時生成的日誌和根據日期生成的日誌能夠存儲在不一樣的目錄文件下。在這種狀況下,系統實時生成的日誌內容都會記錄在file
屬性指定的文件中。所以,該日誌文件名稱不會隨着時間的移動而更改。FileAppender
包含的屬性file
,活動文件的名字會根據fileNamePattern
的值,每隔一段時間改變一次,即每隔一段時間會生成一個日誌文件。<appender name="emergencyLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 寫入日誌內容的文件名稱(目錄) -->
<File>log/check.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 活動文件的名字會根據fileNamePattern的值,每隔一段時間改變一次 -->
<fileNamePattern>log/check.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 每產生一個日誌文件,該日誌文件的保存期限爲30天 -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<!-- pattern節點,用來設置日誌的輸入格式 -->
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %logger [%msg]%n</pattern>
<!-- 記錄日誌的編碼:此處設置字符集 - -->
<charset>UTF-8</charset>
</encoder>
</appender>
複製代碼
以2019-06-04輸出的日誌爲例,來解釋下設置、忽略File屬性的不一樣。app
log/check.log
中。check.log
會被重命名爲log/check.2019-06-04.log
。log/check.2019-06-05.log
、log/check.2019-06-06.log
等日誌。log/check.2019-06-04.log
中。log/check.2019-06-04.log
。仍以2019-06-04爲
例,若是你設置了File
屬性,當天你只能看到check.log日誌文件,2019-06-05
纔會看到check.201-06-04.log
文件。可是若是你忽略了,你當天就能看到check.2019-06-04.log
文件,但你始終看不到check.log文件。ui