logback解析——Appender

在SSM框架搭建Java Web的過程當中,須要去作日誌處理。在配置logback的時候遇到了很多問題,所以而去深刻地瞭解了一下logback。除了看了不少博客的介紹以外,還去看了一下logback的官方文檔,看完以後須要去記錄一下所見所得。html

首先咱們來介紹一個Appender的組成web

Appender的類圖以下: 數組

image.png

ConsoleAppender

ConsoleAppender用於控制檯日誌輸出 官方文檔給出了一個示例寫法bash

<configuration>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
    </encoder>
  </appender>

  <root level="DEBUG">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>
複製代碼
  • <encoder>的做用是將日誌格式化,轉換爲字節數組,並將轉換後的字節數組輸出到OutputStream 在encoder出現以前,主要依靠Layout來處理日誌格式化。

目前,PatternLayoutEncoder惟一真正有用的編碼器。它只是包裝了 PatternLayout大部分工做。 具體詳見https://logback.qos.ch/manual/encoders.htmlapp

FileAppender

FileAppender用於將日誌以文件形式保存起來 官方文檔示例配置以下框架

<configuration>

  <!-- Insert the current time formatted as "yyyyMMdd'T'HHmmss" under
       the key "bySecond" into the logger context. This value will be
       available to all subsequent configuration elements. -->
  <timestamp key="bySecond" datePattern="yyyyMMdd'T'HHmmss"/>

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <!-- use the previously created timestamp to create a uniquely
         named log file -->
    <file>log-${bySecond}.txt</file>
    <encoder>
      <pattern>%logger{35} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="DEBUG">
    <appender-ref ref="FILE" />
  </root>
</configuration>
複製代碼
  • <timestamp>能夠用這個屬性來記錄這個配置文件的解析時間(我暫時還想不到有什麼用)
  • <file>記錄文件的地址和文件名

RollingFileAppender

RollingFileAppender是對FileAppender的一個擴展。相較於它的父類,它的主要做用是滾動記錄日誌。 RollingFileAppender有兩個重要的子組件:RollingPolicy和 TriggeringPolicy。RollingPolicy決定日誌滾動方式,TriggeringPolicy決定日誌滾動的觸發條件。 (其實RollingPolicy也能夠定義滾動的觸發條件)異步

而RollingPolicy滾動策略包括如下幾種:ui

TimeBasedRollingPolicy

基於時間的滾動策略。這個多是最經常使用的滾動策略。 官方文檔示例配置以下編碼

<configuration>
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>logFile.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- daily rollover -->
      <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>

      <!-- keep 30 days' worth of history capped at 3GB total size --> <maxHistory>30</maxHistory> <totalSizeCap>3GB</totalSizeCap> </rollingPolicy> <encoder> <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> </encoder> </appender> <root level="DEBUG"> <appender-ref ref="FILE" /> </root> </configuration> 複製代碼
  • <fileNamePattern>滾動後的文件名,也包括了滾動時間的選擇。
  • <maxHistory>保留的存檔文件的數量,與上一個fileNamePattern有關。假設定義爲6,當fileNamePattern以天爲單位時,即保存6天的日誌;當以月爲單位時,即保存6個月的日誌。舊的日誌以異步的方式刪除。
  • <totalSizeCap>全部的歸檔日誌的大小。當超過限制時,會刪掉舊的歸檔日誌。

SizeAndTimeBasedRollingPolicy

SizeAndTimeBasedRollingPolicy是基於時間和文件大小的滾動策略 官方文檔示例配置以下spa

<configuration>
  <appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>mylog.txt</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
      <!-- rollover daily -->
      <fileNamePattern>mylog-%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
       <!-- each file should be at most 100MB, keep 60 days worth of history, but at most 20GB -->
       <maxFileSize>100MB</maxFileSize>    
       <maxHistory>60</maxHistory>
       <totalSizeCap>20GB</totalSizeCap>
    </rollingPolicy>
    <encoder>
      <pattern>%msg%n</pattern>
    </encoder>
  </appender>


  <root level="DEBUG">
    <appender-ref ref="ROLLING" />
  </root>

</configuration>
複製代碼

<totalSizeCap>限定的是全部的歸檔日誌文件,而<maxFileSize>限定的則是單個日誌文件

FixedWindowRollingPolicy

基於窗口大小的滾動策略。 這個聽起來可能有點難理解,其實說白了就是將歸檔日誌文件到最大了就寫到下一個文件裏,而窗口大小就是最多容許多少份日誌文件。 官方文檔示例配置以下

<configuration>
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>test.log</file>

    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
      <fileNamePattern>tests.%i.log.zip</fileNamePattern>
      <minIndex>1</minIndex>
      <maxIndex>3</maxIndex>
    </rollingPolicy>

    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
      <maxFileSize>5MB</maxFileSize>
    </triggeringPolicy>
    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
  </appender>
        
  <root level="DEBUG">
    <appender-ref ref="FILE" />
  </root>
</configuration>
複製代碼
  • <minIndex>窗口下限。下限通常都是1啦。
  • <maxIndex>窗口上限。通常咱們用上限就能夠了。

SizeBasedTriggeringPolicy

SizeBasedTriggeringPolicy實際上是TriggeringPolicy(決定何時滾動),好像目前暫時也只有這麼一個TriggeringPolicy。 主要做用是歸檔日誌文件到達必定大小以後進行日誌滾動。 根據網上的說法,TimeBasedRollingPolicy和SizeBasedTriggeringPolicy衝突,不能同時使用。

舊版本的logback裏能夠在TimeBasedRollingPolicy這個rollingPolicy下配置一個timeBasedFileNamingAndTriggeringPolicy(實現類爲SizeAndTimeBasedFNATP)達到同時配置時間和文件大小的滾動策略;而在新版本里其實使用SizeAndTimeBasedRollingPolicy就能夠同時知足兩個需求了。

附錄:

下面給出一箇舊版本里Appender的配置:

<!-- 日誌文件輸出 -->
    <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}/${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>
        </rollingPolicy>
        <!-- 日誌輸出的文件的格式  -->
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger{56}.%method:%L -%msg%n</pattern>
        </layout>
    </appender>
複製代碼

而新版本里的SizeAndTimeBasedRollingPolicy配置,官方給出的示例以下:

<configuration>
  <appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>mylog.txt</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
      <!-- rollover daily -->
      <fileNamePattern>mylog-%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
       <!-- each file should be at most 100MB, keep 60 days worth of history, but at most 20GB -->
       <maxFileSize>100MB</maxFileSize>    
       <maxHistory>60</maxHistory>
       <totalSizeCap>20GB</totalSizeCap>
    </rollingPolicy>
    <encoder>
      <pattern>%msg%n</pattern>
    </encoder>
  </appender>


  <root level="DEBUG">
    <appender-ref ref="ROLLING" />
  </root>

</configuration>
複製代碼
相關文章
相關標籤/搜索