slf4j、log4j2及logback使用

2022-10-03 06:03:10

slf4j、log4j2及logback使用

1、問題來源

之前看過關於slf4j、log4j2及logback的介紹,slf4j是門面,log4j2及logback是具體實現,僅使用slf4j門面是無法輸出紀錄檔的。

本文采用jdk11,測試程式碼Slf4jTest.java如下:

import lombok.extern.slf4j.Slf4j;
@Slf4j
public class Slf4jTest {
    public static void main(String[] args) {
        log.info("info");
        log.debug("debug");
        log.error("error");
        log.warn("warn");
    }
}

@Slf4j需要<dependency>依賴如下:

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.24</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>2.0.0</version>
</dependency>

執行程式,不出意外,程式報錯SLF4J: No SLF4J providers were found.。原因為程式碼只有slf4j門面,沒有相關實現。

2、slf4j與log4j2使用

(1)在maven依賴中引入slf4j-log4j12、及log4j-core包,如下圖

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>2.0.0</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.18.0</version>
</dependency>

執行程式碼,之前的錯誤消失,取而代之的是log4j:WARN No appenders could be found for logger (binghe.concurrent.Slf4jTest).錯誤,如下圖。原因為log4j2需要組態檔。

(2)在專案resources目錄下新建log4j.properties檔案,程式碼如下

# Set root logger level to DEBUG and its only appender to console.
log4j.rootLogger=DEBUG, console
# console is set to be a ConsoleAppender.
log4j.appender.console=org.apache.log4j.ConsoleAppender
# console uses PatternLayout.
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.Target = System.out
log4j.appender.console.Threshold = DEBUG
log4j.appender.console.layout.ConversionPattern=%-4r [%t]  %-5p %c %x - %m%n

執行程式碼,程式正常輸出:

0    [main]  INFO  binghe.concurrent.Slf4jTest  - info
1    [main]  DEBUG binghe.concurrent.Slf4jTest  - debug
2    [main]  ERROR binghe.concurrent.Slf4jTest  - error
2    [main]  WARN  binghe.concurrent.Slf4jTest  - warn

3、slf4j與logback使用

刪除第2節中maven依賴引入的slf4j-log4j12、及log4j-core包。

(1)在maven依賴中引入logback-classic、及logback-core

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.4.0</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>1.4.0</version>
</dependency>

(2)在專案resources目錄下新建logback.xml檔案

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %msg%n</pattern>
        </encoder>
    </appender>
    <!-- 下面設定一些第三方包的紀錄檔過濾級別,用於避免刷屏 -->
    <logger name="org.springframework" level="ERROR" />
    <logger name="com.opensymphony" level="ERROR" />
    <logger name="org.quartz" level="ERROR" />
    <logger name="org.apache" level="ERROR" />
    <logger name="net.sf" level="ERROR" />
    <!-- 輸出到控制檯和檔案,可定義更多的 Appender -->
    <root level="DEBUG">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

執行程式碼,程式正常輸出:

2022-10-02 21:13:37.174 INFO  [main] info
2022-10-02 21:13:37.181 DEBUG [main] debug
2022-10-02 21:13:37.181 ERROR [main] error
2022-10-02 21:13:37.181 WARN  [main] warn