Spring Boot 自定義日誌詳解
本節內容基於 Spring Boot 2.0.
你所需具備的基礎
- 什麼是 Spring Boot?
- Spring Boot 核心配置檔案詳解
- Spring Boot 開啟的 2 種方式
- Spring Boot 自動配置原理、實戰
- Spring Boot 2.x 啟動全過程原始碼分析
更多請在Java技術棧微信公眾號後臺回覆關鍵字:boot。
Spring Boot 日誌綜合介紹
Spring Boot 內部程式碼使用的是 commons-logging
來記錄日誌的,但是底層日誌實現框架是可以隨意替換的。Spring Boot為 Java Util Logging
Log4J2
, 和 Logback
日誌框架提供了預設配置。
Spring Boot支援的日誌框架預設配置如下。
# LOGGING logging.config= # Location of the logging configuration file. For instance, `classpath:logback.xml` for Logback. logging.exception-conversion-word=%wEx # Conversion word used when logging exceptions. logging.file= # Log file name (for instance, `myapp.log`). Names can be an exact location or relative to the current directory. logging.file.max-history=0 # Maximum of archive log files to keep. Only supported with the default logback setup. logging.file.max-size=10MB # Maximum log file size. Only supported with the default logback setup. logging.level.*= # Log levels severity mapping. For instance, `logging.level.org.springframework=DEBUG`. logging.path= # Location of the log file. For instance, `/var/log`. logging.pattern.console= # Appender pattern for output to the console. Supported only with the default Logback setup. logging.pattern.dateformat=yyyy-MM-dd HH:mm:ss.SSS # Appender pattern for log date format. Supported only with the default Logback setup. logging.pattern.file= # Appender pattern for output to a file. Supported only with the default Logback setup. logging.pattern.level=%5p # Appender pattern for log level. Supported only with the default Logback setup. logging.register-shutdown-hook=false # Register a shutdown hook for the logging system when it is initialized.
如果不配置以上任何引數,日誌預設只會以 INFO
以上的級別列印在控制檯,不會記錄在日誌檔案中。
如果使用了任何 Starters
,那 Spring Boot 預設會使用 Logback
日誌框架記錄日誌,併為 Logback
提供了支援Java Util Logging
, Commons Logging
, Log4J
, SLF4J
適合的橋接器以便能從這些日誌門面中自由切換。即專案中不管使用哪個日誌門面,Logback都能正常工作。
如下圖,從 spring-boot-starter-web
依賴樹中看出包含了預設日誌框架 Logback
及其他的橋接器。
Spring Boot 日誌實戰
在配置檔案 application.properties
新增以下配置。
# 日誌級別
logging.level.root=DEBUG
# 輸出到日誌檔案
logging.file=d:/logs/javastack.log
# 控制框架中的日誌級別
logging.level.org.springframework=INFO
logging.level.sun=WARN
在 Application
啟動類中新增以下測試程式碼。
private static final org.apache.commons.logging.Log logger1 = org.apache.commons.logging
.LogFactory
.getLog(SpringBootBestPracticeApplication.class);
private static final org.slf4j.Logger logger2 = org.slf4j.LoggerFactory
.getLogger(SpringBootBestPracticeApplication.class);
private static final java.util.logging.Logger logger3 = java.util.logging.Logger
.getLogger("SpringBootBestPracticeApplication");
@Bean
public CommandLineRunner loggerLineRunner() {
return (args) -> {
logger1.error("commons logging error...");
logger1.info("commons logging info...");
logger2.info("slf4j info...");
logger2.info("java util logging info...");
logger1.debug("commons logging debug...");
};
}
日誌輸出如下。
2018-05-24 17:16:21.645 ERROR 3132 --- [ main] c.j.s.SpringBootBestPracticeApplication : commons logging error...
2018-05-24 17:16:21.645 INFO 3132 --- [ main] c.j.s.SpringBootBestPracticeApplication : commons logging info...
2018-05-24 17:16:21.645 INFO 3132 --- [ main] c.j.s.SpringBootBestPracticeApplication : slf4j info...
2018-05-24 17:16:21.645 INFO 3132 --- [ main] c.j.s.SpringBootBestPracticeApplication : java util logging info...
2018-05-24 17:16:21.645 DEBUG 3132 --- [ main] c.j.s.SpringBootBestPracticeApplication : commons logging debug...
程式中使用了三種不同的日誌門面測試,和預設的 Logback
框架工作都十分正常,日誌也正常輸出到指定檔案中了。
Spring Boot 預設提供配置的形式非常簡單,只適合簡單的日誌應用,雖然說日誌輸出格式可以自定義,但日誌檔案如何按天滾動等其他更復雜的策略卻不能配置,只能通過自定義引用日誌檔案的形式。
Spring Boot 定製日誌檔案
簡單的日誌配置不能滿足實際專案需求,那可以通過引用定製日誌檔案的形式達到目的。Spring Boot能根據類路徑下的類庫和配置檔案自動配置對應的日誌框架。
日誌框架 | 配置檔案 |
---|---|
Logback | logback-spring.xml, logback-spring.groovy, logback.xml, or logback.groovy |
Log4j2 | log4j2-spring.xml or log4j2.xml |
JDK (Java Util Logging) | logging.properties |
按對應類庫在 classpath 下建立對應支援的日誌配置檔案就行,或者通過配置 logging.config
指定。
既然預設是支援 Logback
的,那現在只要在資源根目錄下建立一個 logback-spring.xml
檔案即可。xx-spring
這是 Spring Boot 推薦的命名方式,否則 Spring Boot 不能完全控制日誌初始化,因為預設命名配置檔案 logback.xml
載入較早不能獲取到 application.properties
中的配置資訊。
看到這裡,相信你對 Spring Boot 的日誌應該有了一個全面的瞭解。如何使用配置檔案列印日誌和傳統專案一樣,這裡就不囉嗦了。
所有 Spring Boot 文章示例程式碼都在 Github 上面,大家可以 Star 關注一下。
https://github.com/javastacks/spring-boot-best-practice
關注Java技術棧,獲取更多幹貨推送!
本文原創首發於微信公眾號:Java技術棧(id:javastack),關注公眾號在後臺回覆 "boot" 可獲取更多,轉載請原樣保留本資訊。