Spring Boot SLF4j Logback

1k views Asked by At

I have a small SpringBoot MvC app. with those dependencies:

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>${spring-boot-starter-test.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
    </dependencies>

I put a logback-test.xml file in the /src/test/resources/ and when I run the test it works fine

<configuration debug="true" scan="true" scanPeriod="150 seconds">
    <property name="LOG_DIR" value="logs-test" />
    <appender name="FILE_INFO"
            class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_DIR}/app_info.log</file>
        <encoder
                class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} -
                %msg%n
            </Pattern>
....
</configuration>

but when I add to the file /src/test/resources/application.properties:

  # logging level
logging.level.root=error
    logging.level.org.springframework=ERROR
    logging.level.com.plats.bruts=ERROR

but it seems not to work, because I see the DEBUG level on the console when running the test:

10:16:08.039 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class eu.europa.ec.oib.kw.frontoffice.repository.autorisation.AutorisationPersonneRepositoryTest]
10:16:08.047 [main] DEBUG org.springframework.test.context.BootstrapUtils 
1

There are 1 answers

0
s7vr On BEST ANSWER

Make sure to use @SpringBootTest annotation when testing spring boot apps for spring boot to load default logback logging.

For the most part you can configure without needing custom logback xml file and can be managed using application.properties.

For non spring boot test cases you've to provide your own logback xml file.

For custom configuration you should build your file based on base.xml default provided in the spring boot library ( has both file and console appender defined) so you can import the defaults and add specific configuration based on your need.

This way you still manage the default configuration attributes from application properties for spring boot tests. Also rename the file to end with spring extension so spring boot can take control of logging initialization.

Something like

<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    !-- custom configuration goes here --!
</configuration>