Logback file屬性 與 fileNamePattern屬性的關系


https://juejin.cn/post/6844903859865780231

官方原文

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.


翻譯:

  1. RollingFileAppender的file屬性可以設置,也可以忽略。
  2. 如果設置了FileAppender包含的屬性file,系統實時生成的日志和根據日期生成的日志可以存儲在不同的目錄文件下。在這種情況下,系統實時生成的日志內容都會記錄在file屬性指定的文件中。因此,該日志文件名稱不會隨着時間的移動而更改。
  3. 如果忽略了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屬性的不同。

設置File屬性

  1. 系統會將日志內容全部寫入log/check.log中。
  2. 在2019-06-05凌晨,check.log會被重命名為log/check.2019-06-04.log
  3. 然后再生成新的check.log文件,按照上面的步驟生成log/check.2019-06-05.loglog/check.2019-06-06.log等日志。

忽略File屬性

  1. 系統會將日志內容直接寫入log/check.2019-06-04.log中。
  2. 在2019-06-05凌晨,系統會將日志內容直接寫入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文件。


作者:風吟空城
鏈接:https://juejin.cn/post/6844903859865780231
來源:掘金
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM