Spring Boot 測試時的日志級別


1.概覽

該教程中,我將向你展示:如何在測試時設置spring boot 日志級別。雖然我們可以在測試通過時忽略日志,但是如果需要診斷失敗的測試,選擇正確的日志級別是非常重要的。

2.日志級別的重要性

正確設置日志級別可以節省我們許多時間。 舉例來說,如果測試在CI服務器上失敗,但在開發服務器上時卻通過了。我們將無法診斷失敗的測試,除非有足夠的日志輸出。 為了獲取正確數量的詳細信息,我們可以微調應用程序的日志級別,如果發現某個java包對我們的測試更加重要,可以給它一個更低的日志級別,比如DEBUG。類似地,為了避免日志中有太多干擾,我們可以為那些不太重要的包配置更高級別的日志級別,例如INFO或者ERROR。 一起來探索設置日志級別的各種方法吧!

3. application.properties中的日志設置

如果想要修改測試中的日志級別,我們可以在src/test/resources/application.properties設置屬性:

logging.level.com.baeldung.testloglevel=DEBUG

該屬性將會為指定的包com.baeldung.testloglevel設置日志級別。 同樣地,我們可以通過設置root日志等級,更改所有包的日志級別

logging.level.root=INFO

現在通過添加REST端點寫入日志,來嘗試下日志設置。

@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..."; } } 

正如所料,如果我們在測試中調用這個端點,我們將可以看到來自TestLogLevelController的調試日志。

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

這樣設置日志級別十分簡單,如果測試用@SpringBootTest注解,那么我們肯定應該這樣做。但是,如果不使用該注解,則必須以另一種方式配置日志級別。

3.1 基於Profile的日志設置

盡管將配置放在src\test\application.properties在大多數場景下好用,但在某些情況下,我們可能希望為一個或一組測試設置不同的配置。 在這種情況下,我們可以使用@ActiveProfiles注解向測試添加一個Spring Profile:

@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = TestLogLevelApplication.class) @EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class) @ActiveProfiles("logging-test") public class TestLogLevelWithProfileIntegrationTest { // ... } 

日志設置將會存在src/test/resources目錄下的application-logging-test.properties中:

logging.level.com.baeldung.testloglevel=TRACE
logging.level.root=ERROR

如果使用描述的設置調用TestLogLevelCcontroller,將看到controller中打印的TRACE級別日志,並且不會看到其他包出現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默認的Logback,可以在src/test/resources目錄下的logback-text.xml文件中設置日志級別:

<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日志級別設置為INFO,com.baeldung.testloglevel包的日志級別設置為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文件中設置logging.config屬性:

logging.config=classpath:logback-testloglevel.xml

或者,如果想在classpath只有一個的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> 

現在使用logback-test1配置文件調用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屬性來設置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.結論

在本文中,我們學習了如何在Spring Boot測試應用程序時設置日志級別,並探索了許多不同的配置方法。在Spring Boot應用程序中使用application.properties設置日志級別是最簡便的,尤其是當我們使用@SpringBootTest注解時。 與往常一樣,這些示例的源代碼都在GitHub上。

原文鏈接:www.baeldung.com/spring-boot…

作者:baeldung

譯者:Leesen

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM