我的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.
解决方法
<?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.