如何設(shè)置Spring Boot測(cè)試時(shí)的日志級(jí)別
1.概覽
該教程中,我將向你展示:如何在測(cè)試時(shí)設(shè)置spring boot 日志級(jí)別。雖然我們可以在測(cè)試通過(guò)時(shí)忽略日志,但是如果需要診斷失敗的測(cè)試,選擇正確的日志級(jí)別是非常重要的。
2.日志級(jí)別的重要性
正確設(shè)置日志級(jí)別可以節(jié)省我們?cè)S多時(shí)間。
舉例來(lái)說(shuō),如果測(cè)試在CI服務(wù)器上失敗,但在開(kāi)發(fā)服務(wù)器上時(shí)卻通過(guò)了。我們將無(wú)法診斷失敗的測(cè)試,除非有足夠的日志輸出。
為了獲取正確數(shù)量的詳細(xì)信息,我們可以微調(diào)應(yīng)用程序的日志級(jí)別,如果發(fā)現(xiàn)某個(gè)java包對(duì)我們的測(cè)試更加重要,可以給它一個(gè)更低的日志級(jí)別,比如DEBUG。類似地,為了避免日志中有太多干擾,我們可以為那些不太重要的包配置更高級(jí)別的日志級(jí)別,例如INFO或者ERROR。
一起來(lái)探索設(shè)置日志級(jí)別的各種方法吧!
3. application.properties中的日志設(shè)置
如果想要修改測(cè)試中的日志級(jí)別,我們可以在src/test/resources/application.properties設(shè)置屬性:
logging.level.com.baeldung.testloglevel=DEBUG
該屬性將會(huì)為指定的包c(diǎn)om.baeldung.testloglevel設(shè)置日志級(jí)別。
同樣地,我們可以通過(guò)設(shè)置root日志等級(jí),更改所有包的日志級(jí)別
logging.level.root=INFO
現(xiàn)在通過(guò)添加REST端點(diǎn)寫入日志,來(lái)嘗試下日志設(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(cè)試中調(diào)用這個(gè)端點(diǎn),我們將可以看到來(lái)自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í)別十分簡(jiǎn)單,如果測(cè)試用@SpringBootTest注解,那么我們肯定應(yīng)該這樣做。但是,如果不使用該注解,則必須以另一種方式配置日志級(jí)別。
3.1 基于Profile的日志設(shè)置
盡管將配置放在src\test\application.properties在大多數(shù)場(chǎng)景下好用,但在某些情況下,我們可能希望為一個(gè)或一組測(cè)試設(shè)置不同的配置。
在這種情況下,我們可以使用@ActiveProfiles注解向測(cè)試添加一個(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>
以上例子如何在測(cè)試中為L(zhǎng)ogback配置日志級(jí)別。
root日志級(jí)別設(shè)置為INFO,com.baeldung.testloglevel包的日志級(jí)別設(shè)置為DEBUG。
再來(lái)一次,看看提交以上配置后的日志輸出情況
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>
我們可以通過(guò)application.properties中的logging.config屬性來(lái)設(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測(cè)試應(yīng)用程序時(shí)設(shè)置日志級(jí)別,并探索了許多不同的配置方法。在Spring Boot應(yīng)用程序中使用application.properties設(shè)置日志級(jí)別是最簡(jiǎn)便的,尤其是當(dāng)我們使用@SpringBootTest注解時(shí)。
與往常一樣,這些示例的源代碼都在GitHub上。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java中將List拆分為多個(gè)小list集合的實(shí)現(xiàn)代碼
這篇文章主要介紹了Java中如何將List拆分為多個(gè)小list集合,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
淺談Java編程之if-else的優(yōu)化技巧總結(jié)
說(shuō)實(shí)話,其實(shí)我很討厭在代碼里大量使用if-else,一是因?yàn)樵擃惔a執(zhí)行方式屬于面向過(guò)程的,二嘛,則是會(huì)顯得代碼過(guò)于冗余.這篇筆記,主要記錄一些自己在工作實(shí)踐當(dāng)中針對(duì)if-else的優(yōu)化心得,將會(huì)不定期地長(zhǎng)期更新,需要的朋友可以參考下2021-06-06
IntelliJ IDEA中顯示和關(guān)閉工具欄與目錄欄的方法
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA中顯示和關(guān)閉工具欄與目錄欄的方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10
SpringBoot特點(diǎn)之依賴管理和自動(dòng)裝配(實(shí)例代碼)
在使用SpringBoot的時(shí)候,會(huì)自動(dòng)將Bean裝配到IoC容器中,操作也很簡(jiǎn)單,今天小編給大家介紹下SpringBoot特點(diǎn)之依賴管理和自動(dòng)裝配的知識(shí),感興趣的朋友一起看看吧2022-03-03
java9版本特性資源自動(dòng)關(guān)閉的語(yǔ)法增強(qiáng)
這篇文章主要為大家介紹了java9版本特性資源自動(dòng)關(guān)閉的語(yǔ)法增強(qiáng)的詳細(xì)使用說(shuō)明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
Java實(shí)現(xiàn)數(shù)獨(dú)小游戲
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)數(shù)獨(dú)小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
Java獲取漢字拼音的全拼和首拼實(shí)現(xiàn)代碼分享
這篇文章主要介紹了Java獲取漢字拼音的全拼和首拼實(shí)現(xiàn)代碼分享,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-06-06

