java – 如何在测试时设置Spring日志记录级别?

前端之家收集整理的这篇文章主要介绍了java – 如何在测试时设置Spring日志记录级别?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > Spring Boot Test ignores logging.level6个
我的Spring Boot测试堆栈是Maven Surefire JUnit4.我用@RunWith(SpringJUnit4ClassRunner.class)注释测试.

我在我的项目根目录中有application.properties:

logging.level.root=INFO

这可以在运行Spring启动应用程序时控制日志记录,并且可以在正常运行时运行.

但是,每当我运行任何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.jackson.JacksonAutoConfiguration$JacksonObjectMapperConfiguration'
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'
....

所有这些垃圾邮件几乎不可能看到实际相关的部分.如何应用日志记录级别来测试输出

我没有明确设置任何日志记录,并且根据文档默认情况下使用Logback.

解决方法

从一般角度来看,您可以在测试资源级别提供单独的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>

希望这有助于您减少日志输出的路径.更多内容记录在logback自己的页面中:

https://logback.qos.ch/manual/configuration.html

它在顶部提到:

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.

原文链接:https://www.f2er.com/java/130014.html

猜你在找的Java相关文章