如何在@SpringBootTest中防止启动过程中的日志记录噪音


@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = "logging.level.root=OFF")
public class MyTest {
@Test
public void test() {}
}

由于上面的简单测试,我记录了很多启动噪音。在升级到spring-bot-2.x之前,情况并非如此。我该如何防止这种噪音?

特别是,我的Intellij IDE将这些语句记录在red中,随着测试本身的通过,这将更加令人困惑。。。

Jul 31, 2018 1:55:57 PM org.springframework.boot.test.context.SpringBootTestContextBootstrapper buildDefaultMergedContextConfiguration
INFO: Neither @ContextConfiguration nor @ContextHierarchy found for test class [MyTest], using SpringBootContextLoader
Jul 31, 2018 1:55:57 PM org.springframework.test.context.support.AbstractContextLoader generateDefaultLocations
INFO: Could not detect default resource locations for test class [MyTest]: no resource found for suffixes {-context.xml, Context.groovy}.
Jul 31, 2018 1:55:57 PM org.springframework.test.context.support.AnnotationConfigContextLoaderUtils detectDefaultConfigurationClasses
INFO: Could not detect default configuration classes for test class [MyTest]: MyTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
Jul 31, 2018 1:55:57 PM org.springframework.boot.test.context.SpringBootTestContextBootstrapper getOrFindConfigurationClasses
INFO: Found @SpringBootConfiguration MyApp for test class MyTest
Jul 31, 2018 1:55:58 PM org.springframework.boot.test.context.SpringBootTestContextBootstrapper getDefaultTestExecutionListenerClassNames
INFO: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener, org.springframework.security.test.context.support.ReactorContextTestExecutionListener]
Jul 31, 2018 1:55:58 PM org.springframework.boot.test.context.SpringBootTestContextBootstrapper getTestExecutionListeners
INFO: Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@1a4013, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@1b6e1eff, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@306f16f3, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@702b8b12, org.springframework.test.context.support.DirtiesContextTestExecutionListener@22e357dc, org.springframework.test.context.transaction.TransactionalTestExecutionListener@49912c99, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@10163d6, org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener@2dde1bff, org.springframework.security.test.context.support.ReactorContextTestExecutionListener@15bbf42f, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@550ee7e5, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@5f9b2141, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@247d8ae, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@48974e45, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@6a84a97d]

也许这与使用log4j2有关?

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

已编辑

在测试资源中创建logbacktest.xml(\test\resources(,然后将下面的片段放入

<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<logger name="org.springframework" level="OFF"/>
</configuration>

它打印默认的系统INFO,而不是启动噪音日志。(作为你需要(

测试的依赖性可以如下

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

您需要在\test\resources目录中创建一个logbacktest.xml,该文件的内容可以遵循

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

为什么是空的

因为您不希望在这个时间点记录任何内容,所以配置为空。

如果你想了解更多具体信息,你可以在这里看到一些例子

首先:我知道,这不是一个真正的答案,但太长了,无法发表评论。

使用log4j依赖项在我的输出中没有太大变化。

我的POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

我的application.properties是空的,所以没有日志配置。

我的测试

package com.example.demo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = "logging.level.root=")
public class DemoApplicationTests
{
@Test
public void test()
{
System.out.println("Katzenbilder sind doof");
}
}

使用logging.level.root=OFF从IntelliJ中运行测试会导致此输出

.   ____          _            __ _ _
/\ / ___'_ __ _ _(_)_ __  __ _    
( ( )___ | '_ | '_| | '_ / _` |    
\/  ___)| |_)| | | | | || (_| |  ) ) ) )
'  |____| .__|_| |_|_| |___, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot ::        (v2.0.4.RELEASE)
##### Optional[Hallo Welt]
##### Hallo Welt
Katzenbilder sind doof
Process finished with exit code 0

带有#####的两行是来自测试bean的System.out.println((。

使用logging.level.root=INFO运行测试时,我得到了预期的Spring日志消息的混乱。

为了验证起见,我还在application.properties中放入了logging.level.root=INFO,并在测试中将其转换为OFF。没有混乱,只有System.out.println((消息。

这似乎是log4j2的配置问题。请参阅此处log4j2配置的层次结构。

在您的情况下,log4j2最初是使用src/main/resources下可用的log4j2.propertieslog4j2.xml配置的,因此INFO日志会打印在控制台上。只有在加载测试类时,日志记录级别才会更改为OFF,这是在已经打印了启动日志记录噪声之后才完成的。

为了避免任何启动日志噪声,请在src/test/resources下添加log4j2.propertieslog4j2.xmlroot日志级别为ERROR(我不喜欢将其设置为OFF,因为这也会抑制任何错误(

我无法重现启动日志写入stderr的错误。请检查您是否已在log4j2配置中将dest属性设置为err。这可以强制将启动日志写入stderr

编辑#1

分享我放在src/main/resources下的log4j2.xml配置供您参考。

src/test/resources下,<Root level="info">变为<Root level="error">

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="0">
<Properties>
<Property name="LOG_PATTERN">
%d | %5p | [%t] | %c:%M(%L) | %m %n
</Property>
</Properties>
<Appenders>
<Console name="ConsoleAppender">
<PatternLayout pattern="${LOG_PATTERN}" />
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="ConsoleAppender" />
</Root>
</Loggers>
</Configuration>

晚安,这里有很多答案。。关于日志级别配置,所有这些似乎都是有效的。但你的问题可能更大——maven构建更嘈杂——为什么不尝试将surefire的测试输出重定向到单独的文件中,每个测试一个?

https://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#redirectTestOutputToFile

https://maven.apache.org/surefire/maven-failsafe-plugin/integration-test-mojo.html#redirectTestOutputToFile

这可以在全球范围内很好地做到:

<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
</configuration>
</plugin>
</pluginManagement>

最新更新