【问题标题】:Spring Boot Unit Test ignores logging.levelSpring Boot 单元测试忽略 logging.level
【发布时间】:2016-05-15 22:37:06
【问题描述】:

我的一个 maven 模块在运行测试时会忽略我的日志记录级别。

在src/test/resources 我有application.properties:

app.name=bbsng-import-backend
app.description=Import Backend Module for Application
spring.profiles.active=test

# LOGGING
logging.level.root=error
logging.level.org.springframework.core =fatal
logging.level.org.springframework.beans=fatal
logging.level.org.springframework.context=fatal
logging.level.org.springframework.transaction=error
logging.level.org.springframework.test=error
logging.level.org.springframework.web=error
logging.level.org.hibernate=ERROR

我也试过application-test.properties。

我的应用程序记录了很多,尤其是在加载上下文时。我尝试了logback.xml、logback-test.xml 和logback-spring.xml,但没有任何帮助。

我的 pom:

<parent>
    <groupId>at.company.bbsng</groupId>
    <artifactId>bbsng-import</artifactId>
    <version>0.1.0-SNAPSHOT</version>
</parent>

<artifactId>bbsng-import-backend</artifactId>
<name>bbsng-import-backend</name>

<properties>
    <start-class>at.company.bbsng.dataimport.ApplicationImportBackend</start-class>
</properties>


<dependencies>

    <!-- APPLICATION ... -->
    <dependency>
        <groupId>at.company.bbsng</groupId>
        <artifactId>bbsng-app-domain</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- SPRING ... -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-batch</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- JAVAX ... -->
       ...

    <!-- COMMONS ... -->
       ...

    <!-- LOMBOK ... -->
       ...

    <!-- DB -->
       ...

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${org.springframework.boot-version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

一个简单的测试类:

@ContextConfiguration(classes = { ApplicationImportBackend.class })
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles({ "test" })
public class BatchJobConfigurationTests {

    @Autowired
    private JobLauncher jobLauncher;

    @Test
    public void testSimpleProperties() throws Exception {
        assertNotNull(jobLauncher);
    }

}

应用程序日志处于调试模式。

是的,application.properties 将被加载。我已经尝试通过错误的配置来破坏应用程序。

感谢您的任何提示。

【问题讨论】:

    标签: spring-boot logging logback


    【解决方案1】:

    好吧,我现在做了什么,在所有模块中我配置如下:

    src/main/resources:
    我在application.properies 中使用日志配置,如问题中所述的logging.level.*。

    src/test/resources:
    我用logback-test.xml 喜欢:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <include resource="org/springframework/boot/logging/logback/base.xml" />
        <logger name="*.myapp" level="error" />
        <logger name="org.springframework.core " level="error" />
        <logger name="org.springframework.beans" level="error" />
        <logger name="org.springframework.context" level="error" />
        <logger name="org.springframework.transaction" level="error" />
        <logger name="org.springframework.web" level="error" />
        <logger name="org.springframework.test" level="error" />
        <logger name="org.hibernate" level="error" />
    </configuration>
    

    但我仍然不明白,为什么在几个模块中我可以使用 application.properties,但在另一个模块中它忽略了......但现在它对我来说是这样的。

    但也许仍然欢迎一些具有背景知识的提示。

    我没有将我的答案标记为解决方案,因为它仍然感觉像是一种解决方法。

    【讨论】:

    • 我的假设是 application.properties 的解析晚于测试初始化​​。这就是org.springframework.test 对初始测试日志没有影响的原因。
    • 这太棒了。我添加了&lt;logger name="org.springframework.boot" level="warn" /&gt; 和&lt;logger name="org.eclipse.jetty" level="warn" /&gt; 以真正减少噪音。如果您使用的是 swagger,还可以添加 &lt;logger name="springfox" level="warn" /&gt;。做得好。有一个赏金!
    • 这很有帮助,尽管在尝试添加 logback-test.xml 文件后,我开始看到来自 logback 本身的一堆日志。为了关闭 I followed the main answer from this StackOverflow post - 和 BAM,我已经设法在运行测试时摆脱了所有的前期日志记录。
    • 我的logback-test.xml 文件就这么简单:
    • 同样的问题。 2 个具有完全相同配置(基类)的模块。一个在上下文创建期间有日志记录,另一个没有。 application.properties (logging.level) 中的更改生效。使用上面的logback-test.xml 按预期工作。 (+1)谢谢
    【解决方案2】:
    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <include resource="org/springframework/boot/logging/logback/base.xml" />
        <logger name="org.springframework" level="INFO"/>
    </configuration>
    

    作为一种快速修复,我将具有上述内容的logback.xml 文件放在src/test/resources 中,它可以工作。

    【讨论】:

    • 不错,干净的解决方案。该文件也可以命名为“logback-test.xml”,为了清楚起见,应该放在“src/test/resources”下。
    • 是的,清晰是我将其命名为logback-text.xml的原因
    • 我的回答有什么不同?
    【解决方案3】:

    要启用application.properties,需要在测试类中添加注释@SpringBootTest,阅读更多here。

    【讨论】:

    • @SpringBootTest 用于集成测试,因此将加载 application.properties。但是对于单元测试,这不是正确的答案。
    【解决方案4】:

    我也在寻找解决方案,同时我正在使用以下解决方案:

    这不是最好的,但它有效

    @BeforeClass
    public static void setErrorLogging() {
       LoggingSystem.get(ClassLoader.getSystemClassLoader()).setLogLevel(Logger.ROOT_LOGGER_NAME, LogLevel.ERROR);
    }
    

    LoggingSystem:日志系统的通用抽象。

    ->

    get:检测并返回正在使用的日志系统。支持 Logback 和 Java 日志记录

    setLogLevel: 设置给定记录器的记录级别。

    确保更改所有其他测试类的返回日志级别。

    希望对你有帮助,祝你好运

    【讨论】:

    • 这是我停止记录的唯一方法。谢谢
    • 这是一段常见的代码,以防您必须在没有一堆记录器配置文件的情况下测试正在记录的内容。
    • 这就像一个魅力。只想指出如果使用 JUnit5 则使用 @BeforeAll
    【解决方案5】:

    如果您的测试使用 @DataJpaTest 注释,您可以使用以下命令关闭 Hibernate SQL 日志记录:

    @DataJpaTest(showSql=false)
    public class MyTest {
      ..
    }
    

    【讨论】:

      【解决方案6】:

      试试这个:

      @ContextConfiguration(classes = ApplicationImportBackend.class, 
          initializers = ConfigFileApplicationContextInitializer.class)
      @RunWith(SpringJUnit4ClassRunner.class)
      @ActiveProfiles({ "test" })
      public class BatchJobConfigurationTests {
          //...
      }
      

      【讨论】:

      • 感谢您的回复,但调试输出仍然存在。还有胶水吗?
      猜你喜欢
      • 2019-02-05
      • 2020-02-06
      • 2019-05-14
      • 2016-05-28
      • 2015-12-20
      • 1970-01-01
      • 2016-03-15
      • 2017-02-08
      相关资源
      最近更新 更多