Search code examples
spring-bootunit-testingloggingjunitspring-test

How to enable DEBUG level for unit tests in Springboot 3.1.10?


I need to enable debug mode globally, so output from the application classes is logged in both application and unit tests. I enabled DEBUG mode in src\main\resources\application.properties and it works only in the app, but in the tests. BTW, initially there was no logging in tests whatsoever due to No SLF4J providers were found, so I had to add slf4j-simple to pom.xml to fix that, but the log level for test remains at INFO level. I've tried to copy application.properties into src\test\resources, but it didn't help. My app is using java.util.logging.

Would be grateful for any suggestions.


Solution

  • Since unit tests are not running under spring boot and you are using slf4j-simple, you will need to configure Maven surefire to set the correct log level in pom.xml:

         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.2.5</version>
            <configuration>   
              <systemPropertyVariables>
                <!-- logging (also src/test/resources/simplelogger.properties) -->
                <org.slf4j.simpleLogger.defaultLogLevel>DEBUG</org.slf4j.simpleLogger.defaultLogLevel>
     </systemPropertyVariables>
            </configuration>
          </plugin>
    

    You can find other properties to fine tune simple logger in the official docs: https://www.slf4j.org/api/org/slf4j/simple/SimpleLogger.html

    P.S. From experience, every project using slf4j-simple ends up switching to Logback, except in one-off educational exercises.