Spring Boot 之日志記錄
Spring Boot 支持集成 Java 世界主流的日志庫。
如果對於 Java 日志庫不熟悉,可以參考:細說 Java 主流日志工具庫
關鍵詞:
log4j
,log4j2
,logback
,slf4j
Spring Boot 內部日志全部使用 Commons Logging 記錄,但保留底層日志實現。為 Java Util Logging,Log4J2,和 Logback 提供了默認配置。在每種情況下,記錄器都預先配置為使用控制台輸出,並且還提供可選的文件輸出。
默認情況下,如果使用“Starters”,則使用 Logback 進行日志記錄。還包括適當的 Logback 路由,以確保使用 Java Util Logging,Commons Logging,Log4J 或 SLF4J 的依賴庫都能正常工作。
日志格式
Spring Boot 日志默認格式類似下面的形式:
2014-03-05 10:57:51.112 INFO 45469 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/7.0.52
2014-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2014-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1358 ms
2014-03-05 10:57:51.698 INFO 45469 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2014-03-05 10:57:51.702 INFO 45469 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
說明:
- 日期和時間:精確到微秒
- 日志級別:
ERROR
,WARN
,INFO
,DEBUG
, orTRACE
.- 進程 ID
---
分隔符后面是實際的日志內容- 線程名
- 日志名
- 日志內容
控制台輸出
Spring Boot 默認打印信息到控制台,並且僅打印ERROR
, WARN
, INFO
級別信息。
如果你想打印 debug 級別信息,可以設置 jar 啟動參數,如下:
$ java -jar myapp.jar --debug
此外,也可以在 application.properties
中設置 debug = true
。
打印 trace
級別信息同上所示。
彩色打印
如果您的終端支持 ANSI,可以使用彩色打印來提高可讀性。您可以將 spring.output.ansi.enabled 設置為支持的值以覆蓋自動檢測。
使用 %clr 轉換字配置顏色編碼。在最簡單的形式中,轉換器根據日志級別對輸出進行着色,如以下示例所示:
%clr(%5p)
%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){yellow}
支持以下的顏色和樣式:
blue
cyan
faint
green
magenta
red
yellow
文件輸出
默認情況下,Spring Boot 僅記錄到控制台,不會寫入日志文件。如果除了控制台輸出之外還要編寫日志文件,則需要設置 logging.file
或 logging.path
屬性(例如,在 application.properties 中)。
詳細配置參考:配置
日志級別
所有支持的日志系統都可以 在 Spring 環境中通過 logging.level.<logger-name>=<level>
屬性設置日志級別(例如,在 application.properties
中)。其中 level 是 TRACE
、DEBUG
、INFO
、WARN
、ERROR
、FATAL
或 OFF
。可以使用 logging.level.root 配置根記錄器。
示例:
logging.level.root=WARN
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR
日志組
能夠將相關記錄器組合在一起以便可以同時配置它們通常很有用。例如,您可以更改所有 Tomcat 相關記錄器的日志記錄級別,但您無法輕松記住頂級軟件包。
Spring Boot 通過 logging.group 屬性來提供這樣的支持。
logging.group.tomcat=org.apache.catalina, org.apache.coyote, org.apache.tomcat
logging.level.tomcat=TRACE
以下是 Spring Boot 預設的日志組:
名稱 | Loggers |
---|---|
web | org.springframework.core.codec , org.springframework.http , org.springframework.web |
sql | org.springframework.jdbc.core , org.hibernate.SQL |
日志配置文件
可以通過在 classpath 中包含適當的庫來激活各種日志記錄系統,並且可以通過在 classpath 的根目錄中或在以下 Spring Environment
屬性指定的位置提供合適的配置文件來進一步自定義:logging.config
。
您可以使用 org.springframework.boot.logging.LoggingSystem
系統屬性強制 Spring Boot 使用特定的日志記錄系統。該值應該是 LoggingSystem
實現的完全限定類名。您還可以使用 none
值完全禁用 Spring Boot 的日志記錄配置。
由於在創建 ApplicationContext
之前初始化日志記錄,因此無法在 Spring @Configuration
文件中控制來自 @PropertySources
的日志記錄。更改日志記錄系統或完全禁用它的唯一方法是通過系統屬性。
Logback 擴展
profile 指定配置
可以通過 <springProfile>
指定特定的 profile 下的配置,如下:
<springProfile name="staging">
<!-- configuration to be enabled when the "staging" profile is active -->
</springProfile>
<springProfile name="dev | staging">
<!-- configuration to be enabled when the "dev" or "staging" profiles are active -->
</springProfile>
<springProfile name="!production">
<!-- configuration to be enabled when the "production" profile is not active -->
</springProfile>
環境屬性
<springProperty>
標簽允許指定從 Environment
中獲取的屬性,並在配置文件中引用。
<springProperty scope="context" name="fluentHost" source="myapp.fluentd.host"
defaultValue="localhost"/>
<appender name="FLUENT" class="ch.qos.logback.more.appenders.DataFluentAppender">
<remoteHost>${fluentHost}</remoteHost>
...
</appender>
Spring Boot 中的日志配置
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.group.*= # Log groups to quickly change multiple loggers at the same time. For instance, `logging.level.db=org.hibernate,org.springframework.jdbc`.
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.
注:
- 日志配置屬性在應用程序生命周期的早期初始化。因此,通過
@PropertySource
注釋加載的屬性文件中找不到日志記錄屬性。- 日志配置屬性獨立於實際的日志記錄基礎結構。因此,spring Boot 不管理特定的配置密鑰(例如 Logback 的 logback.configurationFile)。
源碼
完整示例:源碼
分別展示如何在 Spring Boot 中使用 log4j
, log4j2
, logback
記錄日志。
引申和引用
引申
引用