SpringBoot中的Logging詳解
前言
log配置可能是被忽視的一個環(huán)節(jié),一般的項目中日志配置好了基本上很少去改動,我們常規(guī)操作是log.info來記錄日志內容,很少會有人注意到springBoot中日志的配置
日志格式
2021-03-09 21:33:06.594 INFO 21236 --- [ main] com.gzb.springboot.logging.LoggingMain : Starting LoggingMain using Java 1.8.0_111 on JT-DS998-pzj with PID 21236 (E:\workspace\springBoot-demo\out\production\classes started by arno.peng in E:\workspace\springBoot-demo)
2021-03-09 21:33:06.597 INFO 21236 --- [ main] com.gzb.springboot.logging.LoggingMain : The following profiles are active: dev
2021-03-09 21:33:08.012 INFO 21236 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-03-09 21:33:08.024 INFO 21236 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-03-09 21:33:08.024 INFO 21236 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.43]
2021-03-09 21:33:08.156 INFO 21236 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-03-09 21:33:08.156 INFO 21236 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1498 ms
2021-03-09 21:33:08.408 INFO 21236 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-09 21:33:08.643 INFO 21236 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-03-09 21:33:08.662 INFO 21236 --- [ main] com.gzb.springboot.logging.LoggingMain : Started LoggingMain in 3.163 seconds (JVM running for 5.555)
如上圖所示是springBoot中默認的日志輸出格式,一般包括幾個部分
- 日期和時間:精確到毫秒,易于排序
- 日志級別:ERROR, WARN, INFO, DEBUG, TRACE
- 進程ID
- [ main]表示線程名稱
- Logger名稱:一般是類的路徑
- 最后是日志的內容
日志輸出
控制臺輸出
默認情況下SpringBoot將日志輸出到控制臺,會輸出INFO、WARN、ERROR這幾個級別的日志,當然還可以通過DEBUG參數來輸出DEBUG日志
比如你在application.yml配置中打開DEBUG日志 debug:true
或者以jar包的形式啟動 java -jar app.jar --debug
都可以以DEBUG模式來啟動項目,此時日志里面會多出很多信息
2021-03-09 21:48:22.581 INFO 2736 --- [ main] com.gzb.springboot.logging.LoggingMain : Starting LoggingMain using Java 1.8.0_111 on JT-DS998-pzj with PID 2736 (E:\workspace\springBoot-demo\out\production\classes started by arno.peng in E:\workspace\springBoot-demo)
2021-03-09 21:48:22.586 INFO 2736 --- [ main] com.gzb.springboot.logging.LoggingMain : The following profiles are active: dev
2021-03-09 21:48:22.590 DEBUG 2736 --- [ main] o.s.boot.SpringApplication : Loading source class com.gzb.springboot.logging.LoggingMain
2021-03-09 21:48:22.681 DEBUG 2736 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@a3d8174
2021-03-09 21:48:24.179 DEBUG 2736 --- [ main] .s.b.w.e.t.TomcatServletWebServerFactory : Code archive: D:\maven\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot\2.4.3\de2bd17a8eb9bc3dfa629aa06f2e9fe3bf603c85\spring-boot-2.4.3.jar
2021-03-09 21:48:24.180 DEBUG 2736 --- [ main] .s.b.w.e.t.TomcatServletWebServerFactory : Code archive: D:\maven\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot\2.4.3\de2bd17a8eb9bc3dfa629aa06f2e9fe3bf603c85\spring-boot-2.4.3.jar
2021-03-09 21:48:24.180 DEBUG 2736 --- [ main] .s.b.w.e.t.TomcatServletWebServerFactory : None of the document roots [src/main/webapp, public, static] point to a directory and will be ignored.
2021-03-09 21:48:24.205 INFO 2736 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-03-09 21:48:24.218 INFO 2736 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-03-09 21:48:24.218 INFO 2736 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.43]
2021-03-09 21:48:24.344 INFO 2736 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-03-09 21:48:24.344 DEBUG 2736 --- [ main] w.s.c.ServletWebServerApplicationContext : Published root WebApplicationContext as ServletContext attribute with name [org.springframework.web.context.WebApplicationContext.ROOT]
2021-03-09 21:48:24.344 INFO 2736 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1663 ms
2021-03-09 21:48:24.364 DEBUG 2736 --- [ main] o.s.b.w.s.ServletContextInitializerBeans : Mapping filters: characterEncodingFilter urls=[/*] order=-2147483648, formContentFilter urls=[/*] order=-9900, requestContextFilter urls=[/*] order=-105
2021-03-09 21:48:24.364 DEBUG 2736 --- [ main] o.s.b.w.s.ServletContextInitializerBeans : Mapping servlets: dispatcherServlet urls=[/]
2021-03-09 21:48:24.432 DEBUG 2736 --- [ main] o.s.b.w.s.f.OrderedRequestContextFilter : Filter 'requestContextFilter' configured for use
2021-03-09 21:48:24.433 DEBUG 2736 --- [ main] s.b.w.s.f.OrderedCharacterEncodingFilter : Filter 'characterEncodingFilter' configured for use
2021-03-09 21:48:24.433 DEBUG 2736 --- [ main] o.s.b.w.s.f.OrderedFormContentFilter : Filter 'formContentFilter' configured for use
2021-03-09 21:48:24.754 INFO 2736 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-09 21:48:24.772 DEBUG 2736 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : ControllerAdvice beans: 0 @ModelAttribute, 0 @InitBinder, 1 RequestBodyAdvice, 1 ResponseBodyAdvice
2021-03-09 21:48:24.877 DEBUG 2736 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : 2 mappings in 'requestMappingHandlerMapping'
2021-03-09 21:48:24.914 DEBUG 2736 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Patterns [/webjars/**, /resources/**] in 'resourceHandlerMapping'
2021-03-09 21:48:24.925 DEBUG 2736 --- [ main] .m.m.a.ExceptionHandlerExceptionResolver : ControllerAdvice beans: 0 @ExceptionHandler, 1 ResponseBodyAdvice
2021-03-09 21:48:24.943 DEBUG 2736 --- [ main] inMXBeanRegistrar$SpringApplicationAdmin : Application Admin MBean registered with name 'org.springframework.boot:type=Admin,name=SpringApplication'
2021-03-09 21:48:25.029 INFO 2736 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
這個只是一部分,后面還有很多SpringBoot自動配置的信息因為篇幅的原因,沒有貼上來 DEBUG模式適用于調試問題,打印更加詳細的信息方便問題的定位
文件輸出
默認情況下,SpringBoot會把日志輸出到控制臺,一般生產環(huán)境會將日志輸出到文件,便于存儲。如果想把日志寫入文件,可以在application.yml配置文件中新增logging.file.name或者logging.file.path配置
- logging.file.name 寫入指定的日志文件,名稱可以是絕對路徑或相對路徑
- logging.file.path 寫入指定的目錄,日志名稱是spring.log,路徑可以是絕對路徑或相對路徑
日志級別
SpringBoot支持的所有日志系統(tǒng)都可以通過在application.yml中設置logging.level來配置日志級別,日志級別有TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF這幾種,使用方式如下
logging:
level:
root: "warn"
org.springframework.web: "debug"
org.hibernate: "error"以上可以為不同的功能模塊設置不同的日志級別,例如將root設置為warn,將hibernate設置為error
日志分組
有時候我們需要對多個功能同時進行日志級別配置,這個時候可以利用日志的分組功能。例如,我們需要設置tomcat相關的日志級別,我們可以先將對應的模塊分組
logging:
group:
tomcat: "org.apache.catalina,org.apache.coyote,org.apache.tomcat"然后對分組進行日志級別的設置
logging:
level:
tomcat: "trace"自定義日志配置
你可以在項目的類路徑上包含適當的日志jar來激活對應的日志記錄系統(tǒng),也可以通過org.springframework.boot.logging.LoggingSystem系統(tǒng)屬性來強制指定SpringBoot來使用指定的日志記錄系統(tǒng),你還可以使用none值完全禁用Spring Boot的日志記錄配置
根據日志系統(tǒng)的不同,加載的文件也不同
| Logging System | Customization |
| 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 |
建議在日志配置中使用-spring變量(例如,logback-spring.xml而不是logback.xml)。 如果使用標準配置位置,Spring不能完全控制日志初始化。
為了方便個性化配置,一些spring日志配置轉換成了系統(tǒng)配置

所有支持的日志記錄系統(tǒng)都支持系統(tǒng)屬性的配置,說白了就是為了簡化配置,將logging配置轉換成系統(tǒng)屬性
具體詳細配置可以參考SpringBoot官方文檔中各個日志系統(tǒng)的用法
- Logback
- Log4j 2
- Java Util logging
Logback擴展
Spring Boot包括許多對Logback的擴展,可以幫助進行高級配置。您可以在配置文件logback-spring.xml中使用這些擴展。
由于標準的logback.xml配置文件加載得太早,因此不能在其中使用擴展。您需要使用logback-spring.xml或定義logging.config屬性
概要文件配置
<springProfile name="staging">
<!-- "staging" profile 被激活時啟用此配置 -->
</springProfile>
<springProfile name="dev | staging">
<!-- "dev" or "staging" profile 被激活時啟用此配置 -->
</springProfile>
<springProfile name="!production">
<!-- 當不是"production"被激活時,啟用此配置 -->
</springProfile>以上標簽可以讓你根據spring的profile激活的配置來選擇性的進行日志配置,name屬性用于指定激活的spring profile,可以正向指定,也可以用排除的方式
環(huán)境配置
<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上下文中的屬性公布出來,可以在logback中使用,如上所示, 定義了參數fluentHost,默認值為localhost,可以直接在配置文件中以${fluentHost}形式引用
好了,日志的部分就這么多,沒有寫太多的例子,大概了解一下后可以根據自己系統(tǒng)的實際情況來進行自定義配置,或者就使用springBoot默認的日志配置也沒太多的問題
到此這篇關于SpringBoot中的Logging詳解的文章就介紹到這了,更多相關SpringBoot的Logging內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- 重學SpringBoot3之日志Logging使用方式
- springboot的logging.group日志分組方法源碼流程解析
- springboot的LogbackLoggingSystem配置加載流程解析
- SpringBoot之配置logging日志及在控制臺中輸出過程
- 解決springboot yml配置 logging.level 報錯問題
- SpringBoot集成slf4j日志配置的方法
- SpringBoot項目的logback日志配置(包括打印mybatis的sql語句)
- 基于logback 實現springboot超級詳細的日志配置
- SpringBoot 下在 yml 中的 logging 日志配置方法
相關文章
使用SpringBoot與Thrift實現RPC通信的方式詳解
在微服務架構的世界里,服務間的通信機制選擇成為了關鍵決策之一,RPC因其簡潔、高效的特點備受青睞,本文將詳細探討如何利用Spring?Boot和Thrift框架構建RPC通信,讓讀者理解其內在原理及實現方式,需要的朋友可以參考下2023-10-10
Spring 事件監(jiān)聽機制實現跨模塊調用的思路詳解
之前一個項目,有兩個模塊,A 模塊需要依賴 B 模塊,但現在 B 模塊有地方需要調用 A 模塊的方法,如果直接依賴,又會產生循環(huán)依賴問題,最終選擇使用 spring 的事件監(jiān)聽來解決該問題,下面給大家介紹Spring 事件監(jiān)聽機制實現跨模塊調用的思路,感興趣的朋友一起看看吧2024-05-05

