【问题标题】:How to disable logging (except errors) in spring junit tests?如何在 spring junit 测试中禁用日志记录(错误除外)?
【发布时间】:2020-01-20 11:18:43
【问题描述】:

我有一个/src/test/resources/application.properties 和一个简单的spring-boot 项目:

spring.main.banner-mode=off
logging.level.root=ERROR
logging.level.org.springframework.*=ERROR

问题:在测试运行期间,我仍然在控制台中看到以下输出:

12:15:33.323 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
12:15:33.373 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
12:15:33.515 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [ServletITest] from class [org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTestContextBootstrapper]
12:15:33.568 [main] INFO org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.slr.hellodocker.HelloServletITest], using SpringBootContextLoader
12:15:33.576 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [ServletITest]: class path resource [ServletITest-context.xml] does not exist
12:15:33.579 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [ServletITest]: class path resource [ServletITestContext.groovy] does not exist
.....

如何完全禁用这些样板日志记录(但不禁用错误日志)?

【问题讨论】:

标签: java spring spring-boot spring-boot-test


【解决方案1】:

如下创建/src/test/resources/logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    <logger name="org.springframework" level="ERROR"/>
</configuration>

【讨论】:

  • 你的意思是我们不能使用 spring boot 中的 application.properties 文件来实现这个?
  • @White_King 至少我没有找到没有将日志记录级别设置为例如OFFERROR 的logback 配置的方法。如果您找到 application.properies 的替代方案,请随时发表评论!
  • 我尝试创建将 logback-test.xml 重命名为 logback.xml,修改 application.yml,创建 boostrap.yml ......似乎没有任何效果
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-28
  • 2017-07-26
  • 2023-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-03
相关资源
最近更新 更多