1.springboot使用log4j2
springboot使用的common-logging,底層兼容各種日志框架如,log4j2,slf4,logback等,默認底層使用的是logback,我們可以去除logback的依賴,引入log4j2的starter,
如下:
2.指定日志配置文件和日志等級
(此配置不限于log4j2,也適用于其他日志框架)
在resources目錄下加入log4j2的xml配置文件,默認spring-boot會加載classpath下面的名為log4j2.xml,或log4j2-file.xml的日志配置文件。
也可以在spring的配置文件中指定需要加載的日志配置文件,以及動態(tài)調(diào)整各個目錄的日志等級
logging: config: classpath:log4j2.xml level: com.ly: debug org.springframework : info
該參數(shù)可以通過系統(tǒng)參數(shù),或啟動參數(shù),覆蓋jar內(nèi)的配置項。
java -jar -Dlogging.config="xxx" test.jar java -jar test.jar --logging.config="xxx"
3.通過springboot-actuator動態(tài)調(diào)整日志級別
(適用于生產(chǎn)環(huán)境)
spring-boot-actuator是springboot的一個監(jiān)控工具,它可以以http或JMX的方式暴露一些endPoint,內(nèi)置的endpoint有 health,info,beans,loggers等。
我們可以通過loggers來動態(tài)調(diào)整日志級別,無需重啟服務(wù)。
如果是想使用webEndPoint的話,項目必須包含web-starter相關(guān)的依賴,因為actuator 的httpEndPoint是以mvc的方式集成的。
3.1 在pom文件中引入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
3.2 在配置文件中開啟loggers的endPoint端點
management: endpoints: web: exposure: include: loggers
3.3 發(fā)起http請求改變?nèi)罩炯墑e
URI默認是 /actuator+endpoint+包名
$ curl 'http://localhost:8080/actuator/loggers/com.example' -i -X POST \ -H 'Content-Type: application/json' \ -d '{"configuredLevel":"debug"}'
原理可以看LoggersEndPoint的實現(xiàn)
4.spring boot日志初始化原理
有個loggingApplicationListener的監(jiān)聽器,監(jiān)聽了spring的事件,讀取了spring容器中的日志配置,進行了日志的初始化。
到此這篇關(guān)于springboot動態(tài)調(diào)整日志級別的文章就介紹到這了,更多相關(guān)springboot調(diào)整日志級別內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/lucky_ly/article/details/120693006