12

I have a /src/test/resources/application.properties with a simple spring-boot project:

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

Problem: during test runs, I still see the following output in console:

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
.....

How can I disable those boilerplate logging entirely (but not error logs)?

membersound
  • 81,582
  • 193
  • 585
  • 1,120

2 Answers2

20

Creating a /src/test/resources/logback.xml as follows:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    <logger name="org.springframework" level="ERROR"/>
</configuration>
membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • 2
    You mean we can't use application.properties file from spring boot to achieve this? – White_King Jun 21 '20 at 11:33
  • 2
    @White_King at least I did not find a way without a logback configuration setting the logging level to eg `OFF` or `ERROR`. Feel free to comment if you find an alternative with application.properies! – membersound Jun 26 '20 at 18:36
  • 2
    I tried creating renaming logback-test.xml to logback.xml, modifying the application.yml, creating a boostrap.yml... nothing seems to work – IcedDante Apr 06 '22 at 18:56
  • That works for me, but a couple of `INFO`-level messages still slip through… only if i combine the above with an `application-test.yml` (or `.properties`-file) as described in https://mkyong.com/spring-boot/spring-boot-test-how-to-stop-debug-logs/ it works. – Philzen Sep 03 '23 at 02:09
0

Suppressing the logs consistently only works using a combination of application[-test].[yml|properties] and logback[-test].xml. The following solution is based on Membersound's answer and this article which was linked in one of the comments above.

The following setup using (1.) an application config file and (2.) a logback config file works for me to suppress everything except errors:

  1. src/test/resources/application-test.yml:

    spring:
      main:
        banner-mode: "off"
    logging:
      level:
        org: "ERROR"
        root: "ERROR"
    

    For those of you not (yet) using yml config files, this is the equivalent

    application-test.properties:

    spring.main.banner-mode=off
    logging.level.root=ERROR
    logging.level.org=ERROR
    
  2. src/test/resources/logback-test.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>
    

One small explanation, in case you're wondering why the above application config does not mention org.springboot at all. The following (longer) yaml gives equivalent results in my project:

spring:
  main:
    banner-mode: "off"
logging:
  level:
    org:
      springframework: "ERROR"
      mongodb: "ERROR"
    root: "ERROR"

… because the project has a mongo db dependency, and without that specific log level value it still output's log messages coming from org.mongodb.driver. The shorter version at the top configures all logs under org.*.

Philzen
  • 3,945
  • 30
  • 46