How to change the log level for unit tests execution only in spring

Tags: , ,



I am having a question on how to change the log level for unit test execution only.

Currently, my application is running (execution, not unit test) with the correct log level, everything is fine, very happy.

However, each time I run unit tests, either on any local machine, either on our CI pipeline, we see the following:

10:29:45.274 [main] DEBUG org.springframework.test.util.ReflectionTestUtils - Setting field 'contactPoints' of type [null] on target object [CassandraConfiguration@231f98ef] or target class [class CassandraConfiguration] to value [localhost]
10:29:45.277 [main] DEBUG org.springframework.test.util.ReflectionTestUtils - Setting field 'port' of type [null] on target object [CassandraConfiguration@231f98ef] or target class [class com.apple.pay.cloud.vaxholm.base.configuration.CassandraConfiguration] to value [9042]

And other debug logs.

What is the proper way to “disable” those debug logs, or “change the log level” for unit tests only please?

Thank you

Answer

if you did not change the default logger of spring you are using logback…

if that is the case

create a logback-test.xml file in src/test/resources

in that file you should be able to configure your logger (most probably the stdout logger

something like:

<configuration>
    <include resource="/org/springframework/boot/logging/logback/base.xml"/>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>
    <root level="error">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>


Source: stackoverflow