如何設(shè)置Spring Boot測試時(shí)的日志級(jí)別
1.概覽
該教程中,我將向你展示:如何在測試時(shí)設(shè)置spring boot 日志級(jí)別。雖然我們可以在測試通過時(shí)忽略日志,但是如果需要診斷失敗的測試,選擇正確的日志級(jí)別是非常重要的。
2.日志級(jí)別的重要性
正確設(shè)置日志級(jí)別可以節(jié)省我們?cè)S多時(shí)間。
舉例來說,如果測試在CI服務(wù)器上失敗,但在開發(fā)服務(wù)器上時(shí)卻通過了。我們將無法診斷失敗的測試,除非有足夠的日志輸出。
為了獲取正確數(shù)量的詳細(xì)信息,我們可以微調(diào)應(yīng)用程序的日志級(jí)別,如果發(fā)現(xiàn)某個(gè)java包對(duì)我們的測試更加重要,可以給它一個(gè)更低的日志級(jí)別,比如DEBUG。類似地,為了避免日志中有太多干擾,我們可以為那些不太重要的包配置更高級(jí)別的日志級(jí)別,例如INFO或者ERROR。
一起來探索設(shè)置日志級(jí)別的各種方法吧!
3. application.properties中的日志設(shè)置
如果想要修改測試中的日志級(jí)別,我們可以在src/test/resources/application.properties設(shè)置屬性:
logging.level.com.baeldung.testloglevel=DEBUG
該屬性將會(huì)為指定的包c(diǎn)om.baeldung.testloglevel設(shè)置日志級(jí)別。
同樣地,我們可以通過設(shè)置root日志等級(jí),更改所有包的日志級(jí)別
logging.level.root=INFO
現(xiàn)在通過添加REST端點(diǎn)寫入日志,來嘗試下日志設(shè)置。
@RestController public class TestLogLevelController { private static final Logger LOG = LoggerFactory.getLogger(TestLogLevelController.class); @Autowired private OtherComponent otherComponent; @GetMapping("/testLogLevel") public String testLogLevel() { LOG.trace("This is a TRACE log"); LOG.debug("This is a DEBUG log"); LOG.info("This is an INFO log"); LOG.error("This is an ERROR log"); otherComponent.processData(); return "Added some log output to console..."; } }
正如所料,如果我們?cè)跍y試中調(diào)用這個(gè)端點(diǎn),我們將可以看到來自TestLogLevelController的調(diào)試日志。
2019-04-01 14:08:27.545 DEBUG 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is a DEBUG log 2019-04-01 14:08:27.545 INFO 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an INFO log 2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an ERROR log 2019-04-01 14:08:27.546 INFO 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an INFO log from another package 2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an ERROR log from another package
這樣設(shè)置日志級(jí)別十分簡單,如果測試用@SpringBootTest注解,那么我們肯定應(yīng)該這樣做。但是,如果不使用該注解,則必須以另一種方式配置日志級(jí)別。
3.1 基于Profile的日志設(shè)置
盡管將配置放在src\test\application.properties在大多數(shù)場景下好用,但在某些情況下,我們可能希望為一個(gè)或一組測試設(shè)置不同的配置。
在這種情況下,我們可以使用@ActiveProfiles注解向測試添加一個(gè)Spring Profile:
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = TestLogLevelApplication.class) @EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class) @ActiveProfiles("logging-test") public class TestLogLevelWithProfileIntegrationTest { // ... }
日志設(shè)置將會(huì)存在src/test/resources目錄下的application-logging-test.properties中:
logging.level.com.baeldung.testloglevel=TRACE logging.level.root=ERROR
如果使用描述的設(shè)置調(diào)用TestLogLevelCcontroller,將看到controller中打印的TRACE級(jí)別日志,并且不會(huì)看到其他包出現(xiàn)INFO級(jí)別以上的日志。
2019-04-01 14:08:27.545 DEBUG 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is a DEBUG log 2019-04-01 14:08:27.545 INFO 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an INFO log 2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an ERROR log 2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an ERROR log from another package
4.配置Logback
如果使用Spring Boot默認(rèn)的Logback,可以在src/test/resources目錄下的logback-text.xml文件中設(shè)置日志級(jí)別:
<configuration> <include resource="/org/springframework/boot/logging/logback/base.xml"/> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n </pattern> </encoder> </appender> <root level="error"> <appender-ref ref="STDOUT"/> </root> <logger name="com.baeldung.testloglevel" level="debug"/> </configuration>
以上例子如何在測試中為Logback配置日志級(jí)別。
root日志級(jí)別設(shè)置為INFO,com.baeldung.testloglevel包的日志級(jí)別設(shè)置為DEBUG。
再來一次,看看提交以上配置后的日志輸出情況
2019-04-01 14:08:27.545 DEBUG 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is a DEBUG log 2019-04-01 14:08:27.545 INFO 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an INFO log 2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an ERROR log 2019-04-01 14:08:27.546 INFO 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an INFO log from another package 2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an ERROR log from another package
4.1 基于Profile配置Logback
另一種配置指定Profile文件的方式就是在application.properties文件中設(shè)置logging.config屬性:
logging.config=classpath:logback-testloglevel.xml
或者,如果想在classpath只有一個(gè)的Logback配置,可以在logbacl.xml使用springProfile屬性。
<configuration> <include resource="/org/springframework/boot/logging/logback/base.xml"/> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n </pattern> </encoder> </appender> <root level="error"> <appender-ref ref="STDOUT"/> </root> <springProfile name="logback-test1"> <logger name="com.baeldung.testloglevel" level="info"/> </springProfile> <springProfile name="logback-test2"> <logger name="com.baeldung.testloglevel" level="trace"/> </springProfile> </configuration>
現(xiàn)在使用logback-test1配置文件調(diào)用TestLogLevelController,將會(huì)獲得如下輸出:
2019-04-01 14:08:27.545 INFO 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an INFO log 2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an ERROR log 2019-04-01 14:08:27.546 INFO 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an INFO log from another package 2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an ERROR log from another package
另一方面,如果更改配置為logback-test2,輸出將變成如下:
2019-04-01 14:08:27.545 DEBUG 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is a DEBUG log 2019-04-01 14:08:27.545 INFO 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an INFO log 2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an ERROR log 2019-04-01 14:08:27.546 INFO 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an INFO log from another package 2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an ERROR log from another package
5.可選的Log4J
另外,如果我們使用Log4J2,我們可以在src\main\resources目錄下的log4j2-spring.xml文件中配置日志等級(jí)。
<Configuration> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" /> </Console> </Appenders> <Loggers> <Logger name="com.baeldung.testloglevel" level="debug" /> <Root level="info"> <AppenderRef ref="Console" /> </Root> </Loggers> </Configuration>
我們可以通過application.properties中的logging.config屬性來設(shè)置Log4J 配置的路徑。
logging.config=classpath:log4j-testloglevel.xml
最后,查看使用以上配置后的輸出:
2019-04-01 14:08:27.545 DEBUG 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is a DEBUG log 2019-04-01 14:08:27.545 INFO 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an INFO log 2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an ERROR log 2019-04-01 14:08:27.546 INFO 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an INFO log from another package 2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an ERROR log from another package
6.結(jié)論
在本文中,我們學(xué)習(xí)了如何在Spring Boot測試應(yīng)用程序時(shí)設(shè)置日志級(jí)別,并探索了許多不同的配置方法。在Spring Boot應(yīng)用程序中使用application.properties設(shè)置日志級(jí)別是最簡便的,尤其是當(dāng)我們使用@SpringBootTest注解時(shí)。
與往常一樣,這些示例的源代碼都在GitHub上。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
log4j控制臺(tái)不打印日志故障的詳細(xì)解決方案
這篇文章主要給大家介紹了關(guān)于log4j控制臺(tái)不打印日志故障的詳細(xì)解決方案,log4j不提供默認(rèn)配置,因?yàn)樵谀承┉h(huán)境中可能禁止輸出到控制臺(tái)或文件系統(tǒng),需要的朋友可以參考下2023-08-08Spring啟動(dòng)后獲取所有擁有特定注解的Bean實(shí)例代碼
這篇文章主要介紹了Spring啟動(dòng)后獲取所有擁有特定注解的Bean實(shí)例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02Java設(shè)計(jì)模式之初識(shí)行為型模式
今天帶大家學(xué)習(xí)Java設(shè)計(jì)模式的相關(guān)知識(shí)點(diǎn),文中對(duì)Java行為型模式做了非常詳細(xì)的介紹及代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下2021-06-06Spring mvc AJAX技術(shù)實(shí)現(xiàn)原理解析
這篇文章主要介紹了Spring mvc AJAX技術(shù)實(shí)現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03詳解Java并發(fā)包中線程池ThreadPoolExecutor
ThreadPoolExecutor是Java語言對(duì)于線程池的實(shí)現(xiàn)。線程池技術(shù)使線程在使用完畢后不回收而是重復(fù)利用。如果線程能夠復(fù)用,那么我們就可以使用固定數(shù)量的線程來解決并發(fā)問題,這樣一來不僅節(jié)約了系統(tǒng)資源,而且也會(huì)減少線程上下文切換的開銷2021-06-06Java之SSM中bean相關(guān)知識(shí)匯總案例講解
這篇文章主要介紹了Java之SSM中bean相關(guān)知識(shí)匯總案例講解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07獲取JPEGImageEncoder和JPEGCode這兩個(gè)類的方法
下面小編就為大家?guī)硪黄@取JPEGImageEncoder和JPEGCode這兩個(gè)類的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07