Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在@SpringBootTest中防止启动过程中出现日志噪音?_Java_Spring_Spring Boot_Spring Boot Test - Fatal编程技术网

Java 如何在@SpringBootTest中防止启动过程中出现日志噪音?

Java 如何在@SpringBootTest中防止启动过程中出现日志噪音?,java,spring,spring-boot,spring-boot-test,Java,Spring,Spring Boot,Spring Boot Test,作为上述简单测试的结果,我记录了很多启动噪音。在升级到spring-boot-2.x之前,情况并非如此。 如何防止这种噪音 特别是,我的Intellij IDE以红色记录这些语句,当测试本身通过时,这会更加混乱 @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = "loggi

作为上述简单测试的结果,我记录了很多启动噪音。在升级到spring-boot-2.x之前,情况并非如此。 如何防止这种噪音

特别是,我的Intellij IDE以红色记录这些语句,当测试本身通过时,这会更加混乱

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
                    properties = "logging.level.root=OFF")
public class MyTest {
         @Test
         public void test() {}
}
也许这与使用log4j2有关

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]

org.springframework.boot
弹簧靴起动器
org.springframework.boot
spring启动程序日志记录
org.springframework.boot
spring-boot-starter-log4j2

首先:我知道,这不是一个真正的答案,但对一个评论来说太长了

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

我的POM

 <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>
从IntelliJ内部运行测试,使用
logging.level.root=OFF
将导致此输出

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");
    }

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

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


为了验证,我还将
logging.level.root=INFO
放在application.properties中,并在测试中关闭它。没有混乱,只有System.out.println()消息。

您需要在\test\resources目录中创建一个logback test.xml,此文件的内容如下


为什么是空的?

因为您不希望在此时记录任何内容,因此-空配置


如果您想了解更多具体信息,可以查看一些已编辑的

在测试资源(\test\resources)中创建logback test.xml, 然后把下面的片段放进去

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.4.RELEASE)

##### Optional[Hallo Welt]
##### Hallo Welt
Katzenbilder sind doof

Process finished with exit code 0

它打印默认系统信息,而不是启动噪音日志。(如你所愿 通缉犯)

测试的依赖性可以如下所示

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

org.springframework.boot
弹簧起动试验
测试

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

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

为了避免任何启动日志噪音,请在
src/test/resources
下添加
log4j2.properties
log4j2.xml
,并将
root
日志级别设置为
ERROR
(我不希望将其设置为
OFF
,因为这也会抑制任何错误)

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

编辑#1

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

src/test/resources
下,
更改为


%d |%5p |[%t]|%c:%M(%L)|%M%n

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

这可以在全球范围内很好地实现,如:

<?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>

org.apache.maven.plugins
maven surefire插件
真的

Use
logging.level.root=off
。它适用于我的Spring Boot v2.0.4.release能否作为测试添加上面添加的
log4j2
依赖项配置?也许这就是原因?我也有同样的问题。您是否尝试过使用Spring进行报告,或者取得了任何其他进展?我可以通过手动指定
@ContextConfiguration(locations=…,classes=…)
@TestExecutionListeners(inheritListeners=true)
来消除几乎所有的输出,但最后一行仍然存在。此外,我想知道为什么这会被记录到
stderr
(红色)而不是
stdout
?您可以使用
spring.main.banner mode=off
禁用横幅。我将尝试此操作,尽管我仍然缺少一些内容-这会使上述消息静音,但也可能隐藏其他有用的消息。信息日志通常应转到标准输出。您知道为什么日志输出显示为红色(stderr)而非白色(stdout)?^无法编辑,但此方法不适用于我。即使存在
logback test.xml
,我仍然在stderr中获得信息输出-我怀疑只有在这些消息出现后才会读取logback配置。感谢您的回答,这是一种使消息静音的方法。但我仍然遗漏了一些东西——这也隐藏了其他有用的信息。信息日志通常应转到标准输出。您知道为什么日志输出显示为红色(stderr)而非白色(stdout)?^无法编辑。当我指定
properties=[“logging.pattern.console=“]
时,日志消息仍然显示在stderr中。
logback test.xml与之相同。很抱歉误解,您的意思是希望控制台中有信息,但处于白色模式?@membersound已编辑,请检查新答案并测试它在
/src/test/resources/logback test.xml
中添加行会更改测试输出:我可以看到白色信息或调试消息
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<?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>
<pluginManagement>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <redirectTestOutputToFile>true</redirectTestOutputToFile>
      </configuration>
  </plugin>
</pluginManagement>