加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 00:54:03 所属栏目:Java 来源:网络整理
导读:参见英文答案 Spring Boot Test ignores logging.level6个 我的Spring Boot测试堆栈是Maven Surefire JUnit4.我用@RunWith(SpringJUnit4ClassRunner.class)注释测试. 我在我的项目根目录中有application.properties: logging.level.root=INFO 这可以在运行S
参见英文答案 > 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-INFservicesch.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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读