SpringBoot中的Logging詳解
前言
log配置可能是被忽視的一個(gè)環(huán)節(jié),一般的項(xiàng)目中日志配置好了基本上很少去改動(dòng),我們常規(guī)操作是log.info來(lái)記錄日志內(nèi)容,很少會(huì)有人注意到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中默認(rèn)的日志輸出格式,一般包括幾個(gè)部分
- 日期和時(shí)間:精確到毫秒,易于排序
- 日志級(jí)別:ERROR, WARN, INFO, DEBUG, TRACE
- 進(jìn)程ID
- [ main]表示線程名稱(chēng)
- Logger名稱(chēng):一般是類(lèi)的路徑
- 最后是日志的內(nèi)容
日志輸出
控制臺(tái)輸出
默認(rèn)情況下SpringBoot將日志輸出到控制臺(tái),會(huì)輸出INFO、WARN、ERROR這幾個(gè)級(jí)別的日志,當(dāng)然還可以通過(guò)DEBUG參數(shù)來(lái)輸出DEBUG日志
比如你在application.yml配置中打開(kāi)DEBUG日志 debug:true
或者以jar包的形式啟動(dòng) java -jar app.jar --debug
都可以以DEBUG模式來(lái)啟動(dòng)項(xiàng)目,此時(shí)日志里面會(huì)多出很多信息
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 ''
這個(gè)只是一部分,后面還有很多SpringBoot自動(dòng)配置的信息因?yàn)槠脑?,沒(méi)有貼上來(lái) DEBUG模式適用于調(diào)試問(wèn)題,打印更加詳細(xì)的信息方便問(wèn)題的定位
文件輸出
默認(rèn)情況下,SpringBoot會(huì)把日志輸出到控制臺(tái),一般生產(chǎn)環(huán)境會(huì)將日志輸出到文件,便于存儲(chǔ)。如果想把日志寫(xiě)入文件,可以在application.yml配置文件中新增logging.file.name或者logging.file.path配置
- logging.file.name 寫(xiě)入指定的日志文件,名稱(chēng)可以是絕對(duì)路徑或相對(duì)路徑
- logging.file.path 寫(xiě)入指定的目錄,日志名稱(chēng)是spring.log,路徑可以是絕對(duì)路徑或相對(duì)路徑
日志級(jí)別
SpringBoot支持的所有日志系統(tǒng)都可以通過(guò)在application.yml中設(shè)置logging.level來(lái)配置日志級(jí)別,日志級(jí)別有TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF這幾種,使用方式如下
logging: level: root: "warn" org.springframework.web: "debug" org.hibernate: "error"
以上可以為不同的功能模塊設(shè)置不同的日志級(jí)別,例如將root設(shè)置為warn,將hibernate設(shè)置為error
日志分組
有時(shí)候我們需要對(duì)多個(gè)功能同時(shí)進(jìn)行日志級(jí)別配置,這個(gè)時(shí)候可以利用日志的分組功能。例如,我們需要設(shè)置tomcat相關(guān)的日志級(jí)別,我們可以先將對(duì)應(yīng)的模塊分組
logging: group: tomcat: "org.apache.catalina,org.apache.coyote,org.apache.tomcat"
然后對(duì)分組進(jìn)行日志級(jí)別的設(shè)置
logging: level: tomcat: "trace"
自定義日志配置
你可以在項(xiàng)目的類(lèi)路徑上包含適當(dāng)?shù)娜罩緅ar來(lái)激活對(duì)應(yīng)的日志記錄系統(tǒng),也可以通過(guò)org.springframework.boot.logging.LoggingSystem系統(tǒng)屬性來(lái)強(qiáng)制指定SpringBoot來(lái)使用指定的日志記錄系統(tǒng),你還可以使用none值完全禁用Spring Boot的日志記錄配置
根據(jù)日志系統(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)。 如果使用標(biāo)準(zhǔn)配置位置,Spring不能完全控制日志初始化。
為了方便個(gè)性化配置,一些spring日志配置轉(zhuǎn)換成了系統(tǒng)配置
所有支持的日志記錄系統(tǒng)都支持系統(tǒng)屬性的配置,說(shuō)白了就是為了簡(jiǎn)化配置,將logging配置轉(zhuǎn)換成系統(tǒng)屬性
具體詳細(xì)配置可以參考SpringBoot官方文檔中各個(gè)日志系統(tǒng)的用法
- Logback
- Log4j 2
- Java Util logging
Logback擴(kuò)展
Spring Boot包括許多對(duì)Logback的擴(kuò)展,可以幫助進(jìn)行高級(jí)配置。您可以在配置文件logback-spring.xml中使用這些擴(kuò)展。
由于標(biāo)準(zhǔn)的logback.xml配置文件加載得太早,因此不能在其中使用擴(kuò)展。您需要使用logback-spring.xml或定義logging.config屬性
概要文件配置
<springProfile name="staging"> <!-- "staging" profile 被激活時(shí)啟用此配置 --> </springProfile> <springProfile name="dev | staging"> <!-- "dev" or "staging" profile 被激活時(shí)啟用此配置 --> </springProfile> <springProfile name="!production"> <!-- 當(dāng)不是"production"被激活時(shí),啟用此配置 --> </springProfile>
以上標(biāo)簽可以讓你根據(jù)spring的profile激活的配置來(lái)選擇性的進(jìn)行日志配置,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>
標(biāo)簽用于將spring上下文中的屬性公布出來(lái),可以在logback中使用,如上所示, 定義了參數(shù)fluentHost,默認(rèn)值為localhost,可以直接在配置文件中以${fluentHost}形式引用
好了,日志的部分就這么多,沒(méi)有寫(xiě)太多的例子,大概了解一下后可以根據(jù)自己系統(tǒng)的實(shí)際情況來(lái)進(jìn)行自定義配置,或者就使用springBoot默認(rèn)的日志配置也沒(méi)太多的問(wèn)題
到此這篇關(guān)于SpringBoot中的Logging詳解的文章就介紹到這了,更多相關(guān)SpringBoot的Logging內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 重學(xué)SpringBoot3之日志Logging使用方式
- springboot的logging.group日志分組方法源碼流程解析
- springboot的LogbackLoggingSystem配置加載流程解析
- SpringBoot之配置logging日志及在控制臺(tái)中輸出過(guò)程
- 解決springboot yml配置 logging.level 報(bào)錯(cuò)問(wèn)題
- SpringBoot集成slf4j日志配置的方法
- SpringBoot項(xiàng)目的logback日志配置(包括打印mybatis的sql語(yǔ)句)
- 基于logback 實(shí)現(xiàn)springboot超級(jí)詳細(xì)的日志配置
- SpringBoot 下在 yml 中的 logging 日志配置方法
相關(guān)文章
Java雜談之代碼重構(gòu)的方法多長(zhǎng)才算長(zhǎng)
關(guān)于代碼重構(gòu)的理解:在不改變軟件系統(tǒng)/模塊所具備的功能特性的前提下,遵循/利用某種規(guī)則,使其內(nèi)部結(jié)構(gòu)趨于完善。其在軟件生命周期中的價(jià)值體現(xiàn)主要在于可維護(hù)性和可擴(kuò)展性2021-10-10深入理解Java虛擬機(jī)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
虛擬機(jī)是一種抽象化的計(jì)算機(jī),通過(guò)在實(shí)際的計(jì)算機(jī)上模擬各種計(jì)算機(jī)功能來(lái)實(shí)現(xiàn)的,下面通過(guò)本文給大家分享Java虛擬機(jī)相關(guān)知識(shí),感興趣的朋友一起看看吧2017-06-06使用SpringBoot與Thrift實(shí)現(xiàn)RPC通信的方式詳解
在微服務(wù)架構(gòu)的世界里,服務(wù)間的通信機(jī)制選擇成為了關(guān)鍵決策之一,RPC因其簡(jiǎn)潔、高效的特點(diǎn)備受青睞,本文將詳細(xì)探討如何利用Spring?Boot和Thrift框架構(gòu)建RPC通信,讓讀者理解其內(nèi)在原理及實(shí)現(xiàn)方式,需要的朋友可以參考下2023-10-10java新人基礎(chǔ)入門(mén)之遞歸調(diào)用
這篇文章主要給大家介紹了關(guān)于java新人基礎(chǔ)入門(mén)之遞歸調(diào)用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02解決Mybatis的@Param()注解導(dǎo)致分頁(yè)失效的問(wèn)題
這篇文章主要介紹了解決Mybatis的@Param()注解導(dǎo)致分頁(yè)失效的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04Springboot使用JustAuth實(shí)現(xiàn)各種第三方登陸
本文主要介紹了Springboot使用JustAuth實(shí)現(xiàn)各種第三方登陸,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Spring 事件監(jiān)聽(tīng)機(jī)制實(shí)現(xiàn)跨模塊調(diào)用的思路詳解
之前一個(gè)項(xiàng)目,有兩個(gè)模塊,A 模塊需要依賴(lài) B 模塊,但現(xiàn)在 B 模塊有地方需要調(diào)用 A 模塊的方法,如果直接依賴(lài),又會(huì)產(chǎn)生循環(huán)依賴(lài)問(wèn)題,最終選擇使用 spring 的事件監(jiān)聽(tīng)來(lái)解決該問(wèn)題,下面給大家介紹Spring 事件監(jiān)聽(tīng)機(jī)制實(shí)現(xiàn)跨模塊調(diào)用的思路,感興趣的朋友一起看看吧2024-05-05