【问题标题】:How to view autoconfigure log output during spring boot tests (integration tests)如何在 Spring Boot 测试(集成测试)期间查看自动配置日志输出
【发布时间】:2015-12-24 09:22:17
【问题描述】:

我正在尝试在测试期间调试 Spring Boot 应用程序,尤其是查看日志输出。

我不确定如何在测试期间获得与运行应用程序时相同的自动配置日志输出

我试过这个(来自src/main/resources/application-test.properties):

logging.level.org.springframework.boot.autoconfigure.test=DEBUG

logging.level.org.springframework.boot.autoconfigure=DEBUG

顺便说一下,我使用 log4j 的配置如下(来自src/main/resources/log4j.properties):

log4j.rootLogger=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

edit:我已经迁移到 logback。这是我的src/main/resources/logback-test.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
        </layout>
    </appender>

    <logger name="org.springframework.boot.autoconfigure" level="debug"/>

    <root level="warn">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

在测试期间我仍然没有得到任何自动配置信息...

【问题讨论】:

    标签: java spring spring-boot log4j spring-test


    【解决方案1】:

    我认为将显式 log4j 配置 (log4j.properties) 与 Spring Boot 结合起来并不是一个好主意。我会使用其中一个。

    当为 org.springframework.boot.autoconfigure.logging 包配置 DEBUG 级别时,会打印自动配置信息。

    在这种情况下,log4j.properties 似乎适用。尝试改变:

    log4j.rootLogger=DEBUG, stdout
    

    或者,如果您决定使用应用程序属性:

    logging.level.org.springframework.boot.autoconfigure.logging=DEBUG
    

    顺便说一句,log4j 是古老的技术。您应该迁移到 LogBack 或 log4j2。

    【讨论】:

      【解决方案2】:

      竞争的日志框架也可能会阻止显示自动配置调试。

      您可能需要排除其中一个才能看到 Spring Boot DEBUG。

      在我的例子中,我在我的 Spring Boot 应用程序中包含了 io.rest-assured:rest-assured 库。 RestAssured 使用 commons-logging 库。我需要排除它才能看到 Spring Boot DEBUG。在我的 build.gradle 中:

      configurations {
          all {
              exclude group: 'commons-logging', module: 'commons-logging'
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-09-09
        • 2017-10-29
        • 1970-01-01
        • 2016-10-10
        • 2021-01-24
        • 2018-12-06
        • 2019-12-19
        • 2017-08-22
        • 2020-02-25
        相关资源
        最近更新 更多