Spring Boot 測試時的日誌級別

1.概覽

該教程中,我將向你展現:如何在測試時設置spring boot 日誌級別。雖然咱們能夠在測試經過時忽略日誌,可是若是須要診斷失敗的測試,選擇正確的日誌級別是很是重要的。java

2.日誌級別的重要性

正確設置日誌級別能夠節省咱們許多時間。 舉例來講,若是測試在CI服務器上失敗,但在開發服務器上時卻經過了。咱們將沒法診斷失敗的測試,除非有足夠的日誌輸出。 爲了獲取正確數量的詳細信息,咱們能夠微調應用程序的日誌級別,若是發現某個java包對咱們的測試更加劇要,能夠給它一個更低的日誌級別,好比DEBUG。相似地,爲了不日誌中有太多幹擾,咱們能夠爲那些不過重要的包配置更高級別的日誌級別,例如INFO或者ERROR。 一塊兒來探索設置日誌級別的各類方法吧!git

3. application.properties中的日誌設置

若是想要修改測試中的日誌級別,咱們能夠在src/test/resources/application.properties設置屬性:github

logging.level.com.baeldung.testloglevel=DEBUG
複製代碼

該屬性將會爲指定的包com.baeldung.testloglevel設置日誌級別。 一樣地,咱們能夠經過設置root日誌等級,更改全部包的日誌級別web

logging.level.root=INFO
複製代碼

如今經過添加REST端點寫入日誌,來嘗試下日誌設置。spring

@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的調試日誌。shell

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:app

@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中:spring-boot

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

相關文章
相關標籤/搜索