Spring Boot SLF4j登录

问题描述

我有一个小型的SpringBoot MvC应用程序。那些依赖项:

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

我在logback-test.xml中放了一个/src/test/resources/文件,运行测试时效果很好

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

但是当我添加到文件/src/test/resources/application.properties:

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

但它似乎不起作用,因为在运行测试时,我在控制台上看到了DEBUG级别:

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 

解决方法

在测试用于春季启动的春季启动应用程序时,请务必使用@SpringBootTest批注来加载默认的Logback日志记录。

在大多数情况下,您可以configure,而无需自定义登录XML文件,并且可以使用application.properties进行管理。

对于非春季启动测试用例,您必须提供自己的logback xml文件。

对于自定义配置,您应该基于spring引导库中提供的base.xml默认值(定义了文件和控制台附加程序)来构建文件,以便您可以导入默认值并根据需要添加特定配置。

这样,您仍然可以从应用程序属性管理default configuration attributes来进行Spring Boot测试。另外,将文件重命名为以spring扩展名结尾,以便spring boot可以控制日志记录初始化。

类似

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...