筆者的操作環(huán)境:
Spring Cloud Alibaba:2022.0.0.0-RC2
Spring Cloud:2022.0.0
Spring Boot:3.0.2
Nacos 2.2.3
Maven 3.8.3
JDK 17.0.7
IntelliJ IDEA 2022.3.1 (Ultimate Edition)
具體步驟
-
因為 Spring Boot 已經(jīng)內(nèi)置了 Logback,所以需要先將 Logback 移除。移除的方法是在 Spring Boot 依賴包中移除 Logback。
<exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions>
比如就像這樣:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency>
【踩坑提醒】
??如何使用了 Maven 多模塊,必須在所有依賴 Logback 的依賴包中將 Logback 排除,否則 Spring Boot 啟動時會發(fā)生如下報錯:
SLF4J: Class path contains multiple SLF4J providers. SLF4J: Found provider [ch.qos.logback.classic.spi.LogbackServiceProvider@4f51b3e0] SLF4J: Found provider [org.apache.logging.slf4j.SLF4JServiceProvider@4b9e255] SLF4J: See https://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual provider is of type [ch.qos.logback.classic.spi.LogbackServiceProvider@4f51b3e0]
??比如,下面這兩個 Spring Boot 依賴包中都依賴了 Logback,所以都要排除 Logback。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency>
??如果想確定自己的 Maven 模塊有沒有依賴 Logback,可以在 IntelliJ IDEA 中查看。
??比方說,下面這種情況就屬于同時依賴了 Logback、Log4j2,這樣 Spring Boot 就會在啟動時報錯。
-
引入與 Spring Boot 適配的 Log4j2 依賴包。
<!-- 設置 SLF4J 與之綁定的日志包。無需提供 SLF4J 的 JAR 包,因為 Lombok 已經(jīng)提供了 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> <version>Spring Boot 的版本</version> </dependency>
此依賴包的版本與 Spring Boot 是一致的。如果不清楚應該使用什么版本,可以去 Maven 倉庫中查詢。Maven 倉庫官網(wǎng):https://mvnrepository.com/
【提示】
??對于不使用 Spring Boot 的項目,使用的是如下經(jīng)典 Log4j2 依賴配置。使用 Spring Boot 之后,此依賴配置是多余的。
<dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>Log4j2 的版本</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>Log4j2 的版本</version> </dependency> <!-- 設置 SLF4J 與之綁定的日志包。無需提供 SLF4J 的 JAR 包,因為 Lombok 已經(jīng)提供了 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>Log4j2 的版本</version> </dependency>
??讀者可以自行檢查,Spring Boot 適配的 Log4j2 依賴包已經(jīng)包含了上述經(jīng)典 Log4j2 依賴配置。
-
提供 Log4j2 的日志配置文件。
如果不提供此配置,則 Spring Boot 會提供一個默認配置。通常,默認配置也不是很糟糕,但它有一個嚴重的問題,它不會將日志輸出至文件來備份。因此,不能使用默認配置。
一個示例的 Log4j2 的日志配置如下,讀者可以自行變更為自己喜歡的配置。
<?xml version="1.0" encoding="UTF-8"?> <configuration status="OFF"> <Properties> <property name="console_log_pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} [%level] [%t] %l %n %m%n</property> <property name="file_log_pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} [%level] [%t] %C.%M[%L line] %n %m%n</property> <property name="every_file_size">20MB</property> </Properties> <appenders> <Console name="Console" target="SYSTEM_OUT"> <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout pattern="${console_log_pattern}"/> </Console> <RollingFile name="DEBUG" fileName="./log/log4j2/debug.log" filePattern="./log/log4j2/debug_log_archive/debug-%d{yyyy-MM-dd}-%i.log.zip"> <PatternLayout pattern="${file_log_pattern}"/> <Policies> <SizeBasedTriggeringPolicy size="${every_file_size}"/> </Policies> <Filters> <ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> </RollingFile> <RollingFile name="INFO" fileName="./log/log4j2/info.log" filePattern="./log/log4j2/info_log_archive/info-%d{yyyy-MM-dd}-%i.log.zip"> <PatternLayout pattern="${file_log_pattern}"/> <Policies> <SizeBasedTriggeringPolicy size="${every_file_size}"/> </Policies> <Filters> <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> </RollingFile> <RollingFile name="WARN" fileName="./log/log4j2/warn.log" filePattern="./log/log4j2/warn_log_archive/warn-%d{yyyy-MM-dd}-%i.log.zip"> <PatternLayout pattern="${file_log_pattern}"/> <Policies> <SizeBasedTriggeringPolicy size="${every_file_size}"/> </Policies> <Filters> <ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> </RollingFile> <RollingFile name="ERROR" fileName="./log/log4j2/error.log" filePattern="./log/log4j2/error_log_archive/error-%d{yyyy-MM-dd}-%i.log.zip"> <PatternLayout pattern="${file_log_pattern}"/> <Policies> <SizeBasedTriggeringPolicy size="${every_file_size}"/> </Policies> <Filters> <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> </RollingFile> </appenders> <loggers> <!-- 屬性 level 是用于設置最低需要輸出的日志輸出級別 --> <root level="DEBUG"> <appender-ref ref="Console"/> <appender-ref ref="DEBUG"/> <appender-ref ref="INFO"/> <appender-ref ref="WARN"/> <appender-ref ref="ERROR"/> </root> </loggers> </configuration>
-
Log4j2 的日志配置文件編寫完成之后,可以放在 Maven 模塊的
resource
目錄下,如下圖所示。然后,在 Spring Boot 配置文件(如
application.yml
)中使用如下代碼引入該配置。logging: config: classpath:log4j2.xml
如果 Log4j2 的日志配置文件名為
log4j2.xml
或log4j2-spring.xml
,且放在resource
目錄下,那就算是不在 Spring Boot 配置文件中引入此 Log4j2 的日志配置,Spring Boot 也會自動讀取該 Log4j2 的日志配置。不過,最好還是顯式地引入此配置。
【注意】
??如果使用了 Maven 多模塊,則此 Log4j2 的日志配置文件和 Spring Boot 配置文件只能放在 Spring Boot 入口模塊中。不要在一種沒有程序啟動入口的 Maven 庫模塊中放置此配置文件。
-
前面已經(jīng)導入了 Log4j2,現(xiàn)在來考慮 SLF4J。
SLF4J 是一種門面日志,它只要發(fā)現(xiàn)導入了 Log4j2,它就會自動使用它。
幸運的是,有一個眾所周知的插件叫 Lombok,它已經(jīng)內(nèi)置了 SLF4J。因此只要使用 Lombok,就可以不需要引入 SLF4J 依賴。
-
引入 Lombok 的依賴代碼如下。
<!-- 注意:Lombok 不會在依賴中被繼承 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>Lombok 的版本</version> <scope>provided</scope> </dependency>
-
然后,在需要使用 SLF4J 的類上使用注解
@Slf4j
。這樣就可以直接在代碼中使用log.xxx(...)
來使用 Log4j2 日志了。@Slf4j public class UserAvatarController { // ...省略其它內(nèi)容... public void fun(HttpServletRequest request, HttpServletResponse response) { log.info("fun called"); } }
附錄
-
Log4j2
-
Log4j2 的開源地址:https://github.com/apache/logging-log4j2
-
Log4j2 官方文檔:https://logging.apache.org/log4j/2.x/manual/index.html
-
-
SLF4J
-
SLF4J 官網(wǎng):https://www.slf4j.org/文章來源:http://www.zghlxwxcb.cn/news/detail-635898.html
-
SLF4J 官方文檔:https://www.slf4j.org/docs.html文章來源地址http://www.zghlxwxcb.cn/news/detail-635898.html
-
到了這里,關于如何在 Spring Boot 中集成日志框架 SLF4J、Log4j的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!