Spring Boot Test忽略logging.level

Mic*_*ner 74 logging ignore logback spring-boot

我的一个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
Run Code Online (Sandbox Code Playgroud)

我也尝试过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>
Run Code Online (Sandbox Code Playgroud)

一个简单的测试类:

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

    @Autowired
    private JobLauncher jobLauncher;

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

}
Run Code Online (Sandbox Code Playgroud)

应用程序日志处于DEBUG模式.

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

谢谢你的任何提示.

Mic*_*ner 71

好的,我现在所做的,在我配置的所有模块中如下:

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>
Run Code Online (Sandbox Code Playgroud)

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

但也许很少有关于背景知识的提示仍然受到欢迎.

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

  • 我的假设是`application.properties`在测试初始化​​之后被解析.这就是`org.springframework.test`对初始测试记录没有影响的原因. (4认同)
  • 这太棒了.我添加了`<logger name ="org.springframework.boot"level ="warn"/>`和`<logger name ="org.eclipse.jetty"level ="warn"/>`来真正降低噪音.如果你使用swagger,你也可以添加`<logger name ="springfox"level ="warn"/>`.做得好.有赏金! (3认同)
  • 这很有帮助,尽管在我尝试添加“logback-test.xml”文件后,我开始看到来自 logback 本身的一堆日志。要关闭这些功能[我遵循了此 StackOverflow 帖子的主要答案](/sf/ask/228000811/ start-of-every-log) - 然后砰的一声,我在运行测试时设法摆脱了所有的前兆日志记录。 (2认同)

dno*_*ode 22

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    <logger name="org.springframework" level="INFO"/>
</configuration>
Run Code Online (Sandbox Code Playgroud)

作为一个快速修复,我把logback.xml文件与上面的内容放在src/test/resources中,它可以工作.

  • 干净整洁的解决方案.该文件也可以命名为"logback-test.xml",为了清楚起见,应该放在'src/test/resources'下. (2认同)
  • 和我的答案有什么区别? (2认同)

小智 21

要启用application.properties,需要在测试类中添加注释@SpringBootTest,在此处阅读更多内容

  • @SpringBootTest 用于集成测试,因此将加载 application.properties。但对于单元测试来说,这不是正确的答案。 (2认同)

小智 7

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

这不是最好的,但是有效

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

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

->

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

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

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

希望它对您有帮助,祝您好运

  • 这是我停止记录的唯一方法。谢谢 (3认同)

uı6*_*uɐp 6

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

@DataJpaTest(showSql=false)
public class MyTest {
  ..
}
Run Code Online (Sandbox Code Playgroud)