java 日誌框架總結
阿新 • • 發佈:2019-01-22
簡介
- commons-logging(jcl) : apache 提供的日誌框架介面,spring 預設使用的框架介面。現在已經很久沒有更新了。
- slf4j : 也是一種日誌框架介面,可以自由的選擇具體的日誌框架。和 commons-logging 實現方法不同,持續更新中。
- java.util.logging(jul) : java 自帶的一個簡單的日誌框架。
- logback : 從 log4j 1.x 版本繼承發展過來的,目的的是提供比log4j 1.x 更好的解決方案。它也是 slf4j 提供的框架介面的本地實現。
- log4j : 有 1.x 和 2.x 兩個版本,1.x 版本較差因此產生了 logback ,而 2.x 版本進行了較大的改進,並且2.x 版本也在向日志框架介面方向發展,支援了對 jul 的適配。
日誌框架的使用分為三種情況:
- 單獨使用 log4j 1.x , jul ,只需要依賴相應的 jar 包,但是程式和具體的日誌框架直接耦合,不利於以後的擴充套件。
- 使用slf4j 或者 jcl 作為日誌介面,選擇 logback , log4j , jul 中的一種作為具體的實現。這種方法是最好的選擇。程式不與具體的日誌框架耦合。
- 程式的部分舊模組採用了第一種方法(跟糟糕的情況可能不能的模組採用的是不同的日誌框架),現在想要用第二種方法。
maven 配置
單獨使用log4j 1.x
<dependency>
<groupId>log4j</groupId >
<artifactId>log4j</artifactId>
<version>{log4j.version}</version>
</dependency>
單獨使用log4j 2.x
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>{log4j.version}</version >
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>{log4j.version}</version>
</dependency>
單獨使用logback(logback-classic 模組是 slf4j 的本地實現,因此必須依賴 slf4j )。
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>{logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>{logback.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>{slf4j.version}</version>
</dependency>
slf4j 作為日誌介面框架
logback 作為slf4j 提供的介面本地實現不需要增加適配層外,log4j 和 jul 都需要新增適配層:
- 與 log4j 1.x 整合
If you wish to use log4j as the underlying logging framework, all you need to do is to declare "org.slf4j:slf4j-log4j12" as a dependency in your pom.xml file as shown below. In addition to slf4j-log4j12-1.7.25.jar, this will pull slf4j-api-1.7.25.jar as well as log4j-1.2.17.jar into your project. Note that explicitly declaring a dependency on log4j-1.2.17.jar or slf4j-api-1.7.25.jar is not wrong and may be necessary to impose the correct version of said artifacts by virtue of Maven's "nearest definition" dependency mediation rule.
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
- 與 log4j 2.x 整合
除 slf4j 和 log4j 的 jar 包之外還需要新增 log4j 對 slf4j 的橋接:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>{log4j-slf4j-impl.version}</version>
</dependency>
- 與 jul 整合
If you wish to use java.util.logging as the underlying logging framework, all you need to do is to declare "org.slf4j:slf4j-jdk14" as a dependency in your pom.xml file as shown below. In addition to slf4j-jdk14-1.7.25.jar, this will pull slf4j-api-1.7.25.jar into your project. Note that explicitly declaring a dependency on slf4j-api-1.7.25.jar is not wrong and may be necessary to impose the correct version of said artifact by virtue of Maven's "nearest definition" dependency mediation rule.
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.25</version>
</dependency>
使用 commons-logging 作為介面框架
- 與 jul 整合
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>{commons-logging.version}</version>
</dependency>
- 與 log4j 1.x 整合
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>{commons-logging.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>{log4j.version}</version>
</dependency>
- 與 log4j 2.x 整合
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>{commons-logging.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>{log4j-api.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>{log4j-core.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>{log4j-jcl.version}</version>
</dependency>
- 與 logback 整合
由logback 提供了支援 commons-logging 語法的 jar,修改了 commons-logging 的部分內容。
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.3</version>
</dependency>
日誌之間切換
- log4j 1.x 切換到 logback : 去掉 log4j,新增 logback-core,logback-classic,slf4j-api,log4j-over-logback 。
- jul 切換到 logback : 新增 jul-to-slf4j,logback-core,logback-classic,slf4j-api。
- commons-logging 切換到 logback : 去掉commons-logging,新增 logback-core,logback-classic,slf4j-api,jcl-over-logback。
- slf4j 切換到 commons-logging: slf4j 給出了採用 slf4j 的 API 之後而不得不使用commons-logging 時,可以通過新增 slf4j-jcl 來將日誌呼叫委派給 jcl。
混合使用多種框架整合
slf4j 官方提供了一下幾種情況的整合方法:
包衝突
slf4j-jcl 與 jcl-over-slf4j : slf4j-jcl 提供了將日誌呼叫委派給 jcl ,而 jcl-over-slf4j 將呼叫委派給 slf4j ,形成相互委託。同理還有:
- log4j-over-slf4j 與 slf4j-log4j12
- jul-to-slf4j 與 slf4j-jdk14
總結
- 目前logback和log4j 2.x 並駕齊驅,所以對於新專案而言,兩者都可以選擇。而commons-logging 和 log4j 1.x 都不在更新,所以新專案並不考慮。
- 考慮到對專案依賴的相容性,老的專案依賴的日誌元件可能是多種多樣的。而 slf4j 給出了相應的解決方案,所以是最優選擇。