本節內容基於 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 關注一下。
關注Java技術棧,獲取更多幹貨推送!
本文原創首發於公衆號:Java技術棧(id:javastack),關注公衆號在後臺回覆 "boot" 可獲取更多,轉載請原樣保留本信息。