在SSM框架搭建Java Web的過程當中,須要去作日誌處理。在配置logback的時候遇到了很多問題,所以而去深刻地瞭解了一下logback。除了看了不少博客的介紹以外,還去看了一下logback的官方文檔,看完以後須要去記錄一下所見所得。html
首先咱們來介紹一個Appender的組成web
Appender的類圖以下: 數組
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>
複製代碼
目前,PatternLayoutEncoder惟一真正有用的編碼器。它只是包裝了 PatternLayout大部分工做。 具體詳見https://logback.qos.ch/manual/encoders.htmlapp
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>
複製代碼
RollingFileAppender是對FileAppender的一個擴展。相較於它的父類,它的主要做用是滾動記錄日誌。 RollingFileAppender有兩個重要的子組件:RollingPolicy和 TriggeringPolicy。RollingPolicy決定日誌滾動方式,TriggeringPolicy決定日誌滾動的觸發條件。 (其實RollingPolicy也能夠定義滾動的觸發條件)異步
而RollingPolicy滾動策略包括如下幾種:ui
基於時間的滾動策略。這個多是最經常使用的滾動策略。 官方文檔示例配置以下編碼
<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> 複製代碼
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>限定的則是單個日誌文件
基於窗口大小的滾動策略。 這個聽起來可能有點難理解,其實說白了就是將歸檔日誌文件到最大了就寫到下一個文件裏,而窗口大小就是最多容許多少份日誌文件。 官方文檔示例配置以下
<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>
複製代碼
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>
複製代碼