java 测试时如何设置Spring日志记录级别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44237243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set Spring logging level while testing?
提问by Hubert Grzeskowiak
My Spring Boot testing stack is Maven + Surefire + JUnit4. I am annotating the tests with @RunWith(SpringJUnit4ClassRunner.class)
.
我的 Spring Boot 测试堆栈是 Maven + Surefire + JUnit4。我正在用@RunWith(SpringJUnit4ClassRunner.class)
.
I have application.properties
in my project root with this line:
我application.properties
在我的项目的根这一行:
logging.level.root=INFO
This controls the logging when running the Spring boot app and it works on normal runs.
这控制了运行 Spring boot 应用程序时的日志记录,并且可以正常运行。
However, whenever I run any JUnit4 tests, I am spammed by pages of DEBUG output like this:
然而,每当我运行任何 JUnit4 测试时,我都会收到像这样的 DEBUG 输出页面的垃圾邮件:
....
17:43:20.500 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'autoConfigurationReport'
17:43:20.500 [main] DEBUG org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader - Registered bean definition for imported class 'org.springframework.boot.autoconfigure.Hymanson.HymansonAutoConfiguration$HymansonObjectMapperConfiguration'
17:43:20.501 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.boot.autoconfigure.condition.BeanTypeRegistry'
17:43:20.502 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'autoConfigurationReport'
....
All this spam makes it almost impossible to see the actually relevant parts. How can I apply the logging levels to test output?
所有这些垃圾邮件几乎不可能看到实际相关的部分。如何将日志记录级别应用于测试输出?
I haven't set any logging explicitly, and according to the docs Logback is used by default.
我没有明确设置任何日志记录,根据文档,默认使用 Logback。
回答by vegaasen
From a general perspective, you can provide a seperate logback-test.xml
-file at the test-resource level. In this file you can add settings regarding the log-level targeted at the output you'd like such as:
从一般的角度来看,您可以logback-test.xml
在测试资源级别提供一个单独的文件。在此文件中,您可以添加有关针对您想要的输出的日志级别的设置,例如:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</layout>
</appender>
<logger name="com.your.package" level="DEBUG">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="org.springframework" level="WARN">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="org.hibernate" level="WARN">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="org.eclipse" level="WARN">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="jndi" level="WARN">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="org.apache.http.wire" level="WARN">
<appender-ref ref="CONSOLE"/>
</logger>
<root level="DEBUG">
<appender-ref ref="CONSOLE"/>
</root>
</configuration>
Hope this helps you somewhat on the path to decreasing the log output. More is documented at logback's own page:
希望这对您减少日志输出的路径有所帮助。更多信息记录在 logback 自己的页面上:
https://logback.qos.ch/manual/configuration.html
https://logback.qos.ch/manual/configuration.html
Its mentioned in the top section:
它在顶部提到:
Let us begin by discussing the initialization steps that logback follows to try to configure itself: 1.Logback tries to find a file called logback-test.xml in the classpath.
2.If no such file is found, logback tries to find a file called logback.groovy in the classpath.
3.If no such file is found, it checks for the file logback.xml in the classpath..
4.If no such file is found, service-provider loading facility (introduced in JDK 1.6) is used to resolve the implementation of com.qos.logback.classic.spi.Configurator interface by looking up the file META-INF\services\ch.qos.logback.classic.spi.Configurator in the class path. Its contents should specify the fully qualified class name of the desired Configurator implementation.
5.If none of the above succeeds, logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.
让我们从讨论 logback 尝试配置自身所遵循的初始化步骤开始: 1.Logback 尝试在类路径中找到一个名为 logback-test.xml 的文件。
2.如果没有找到这样的文件,logback 会尝试在类路径中找到一个名为 logback.groovy 的文件。
3.如果没有找到这样的文件,它会检查类路径中的文件logback.xml..
4.如果没有找到该文件,则使用service-provider加载工具(JDK 1.6中引入)通过查找文件META-INF\services\来解析com.qos.logback.classic.spi.Configurator接口的实现类路径中的ch.qos.logback.classic.spi.Configurator。其内容应指定所需配置器实现的完全限定类名。
5.如果以上都没有成功,logback 会使用 BasicConfigurator 自动配置自己,这将导致日志输出被定向到控制台。