1. 程式人生 > >The logback manual #03# Configuration

The logback manual #03# Configuration

(粗糙選譯。。)

Joran是logback所依賴的配置框架。

Configuration in logback

觀察表明,在一個應用程式中,日誌程式碼大概佔4%左右。

所以即便是一個幾千行。。幾萬行的程式也有成百上千的日誌語句,鑑於它們的數量,我們需要工具來管理這些日誌語句。logback既可以通過程式設計方式配置,也可以通過指令碼配置(XML或Groovy 格式)。順便說一下,現有的log4j使用者可以使用PropertiesTranslator 應用程式將log4j.properties轉換為logback.xml

讓我們從討論logback嘗試配置自身的初始化步驟開始:

  1. Logback嘗試在classpath中查詢一個名為logback-test.xml的檔案。
  2. 如果沒找到,logback將嘗試在classpath中檢查有沒有logback.groovy
  3. 如果還是沒有找到,logback繼續在classpath中找logback.xml
  4. 如果依然沒找到,則使用service-provider loading facility(JDK 1.6中引入的)通過查詢類路徑中的檔案META-INF\services\ch.qos.logback.classic.spi.Configurator來解析com.qos.logback.classic.spi.Configurator
    介面
    的實現。它的內容應該指定所需Configurator實現的完全限定類名。
  5. 如果以上方法都不成功,logback將使用BasicConfigurator自動配置自己,這將導致日誌輸出定向到控制檯。

Automatically configuring logback

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.sample.logback</groupId> <artifactId>test-logback</artifactId> <version>1.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- -source 1.5 中不支援 try-with-resources--> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.2.3</version> </dependency> <!--Failed to load class "org.slf4j.impl.StaticLoggerBinder".--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.21</version> </dependency> </dependencies> </project>
pom.xml

BasicConfigurator是logback預設的一個最小可用的配置。試了下文件裡的例子程式,在main函式裡跑,debug資訊莫名奇妙輸不出來(只輸出info的),但在junit裡跑卻又可以。。

Automatic configuration with logback-test.xml or logback.xml

和沒有xml檔案時的預設配置等價的最小可用xml配置:

<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>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="debug">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

Automatic printing of status messages in case of warning or errors

啟用“觀察logback內部狀態模式”(如果logback出現內部錯誤則不啟用也會自動輸出到控制檯):

        // assume SLF4J is bound to logback in the current environment
        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
        // print logback's internal status
        StatusPrinter.print(lc);

Status data

啟用“觀察logback內部狀態模式”通常對診斷logback問題大有幫助。因此,強烈建議將其視為第一求助辦法。(一般情況都是推薦啟用的。)xml啟用“觀察logback內部狀態模式”:

<configuration debug="true"> 

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 
    <!-- encoders are  by default assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="debug">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

等價寫法:

<configuration>
  <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />  

  ... the rest of the configuration file  
</configuration>

Automatically reloading configuration file upon modification

30秒(預設單位是毫秒)掃描一次,如果檔案改變就自動重新配置:

<configuration scan="true" scanPeriod="30 seconds" > 
  ...
</configuration> 

在編輯xml的時候很容易出錯,所以感覺不是很好用。

Enabling packaging data in stack traces

Viewing status messages

通過網頁來檢視logback的內部狀態資訊。

Listening to status messages

"logback.statusListenerClass" system property

Stopping logback-classic

Configuration file syntax