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

