Spring Boot 支持集成 Java 世界主流的日誌庫。html
若是對於 Java 日誌庫不熟悉,能夠參考:細說 Java 主流日誌工具庫java
關鍵詞:
log4j
,log4j2
,logback
,slf4j
git
<!-- TOC depthFrom:2 depthTo:3 -->github
<!-- /TOC -->web
Spring Boot 內部日誌所有使用 Commons Logging 記錄,但保留底層日誌實現。爲 Java Util Logging,Log4J2,和 Logback 提供了默認配置。在每種狀況下,記錄器都預先配置爲使用控制檯輸出,而且還提供可選的文件輸出。spring
默認狀況下,若是使用「Starters」,則使用 Logback 進行日誌記錄。還包括適當的 Logback 路由,以確保使用 Java Util Logging,Commons Logging,Log4J 或 SLF4J 的依賴庫都能正常工做。sql
Spring Boot 日誌默認格式相似下面的形式:apache
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
級別信息。api
若是你想打印 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
的日誌記錄。更改日誌記錄系統或徹底禁用它的惟一方法是經過系統屬性。
能夠經過 <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>
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
記錄日誌。
引伸
引用
原文出處:https://www.cnblogs.com/jingmoxukong/p/10193303.html