membersound
membersound

Reputation: 86935

How to disable logging (except errors) in spring junit tests?

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)?

Upvotes: 15

Views: 13052

Answers (2)

Philzen
Philzen

Reputation: 4697

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

Upvotes: 0

membersound
membersound

Reputation: 86935

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>

Upvotes: 22

Related Questions