如何在gradle引导测试中关闭关闭钩子的输出?


13

您可以从https://start.spring.io/starter.zip?type=gradle-project&language=java&bootVersion=2.2.5.RELEASE&baseDir=demo&groupId=com.example&artifactId=demo&name生成从start.spring.io到此问题的项目= demo&d​​escription = Demo%20project%20for%20Spring%20Boot&packageName = com.example.demo&packaging = jar&javaVersion = 1.8&dependencies = h2,data-jpa,web

我有一个用gradle构建的多模块springBoot应用程序,其中有很多SpringBoot集成测试。当我进行构建时,最终会从SpringBoot关闭到控制台获得一些输出,如下所示。如何关闭此输出?

± |master 1 {1} S:3 U:10 ✗|  ./gradlew build

> Task :core:test
2020-02-01 11:20:33.529  INFO 24114 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-02-01 11:20:33.531  INFO 24114 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-02-01 11:20:33.538  INFO 24114 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

> Task :email:test
2020-02-01 11:20:43.820  INFO 24150 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-02-01 11:20:43.820  INFO 24150 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-02-01 11:20:43.822  INFO 24150 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-2 - Shutdown initiated...
2020-02-01 11:20:43.822  INFO 24150 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-02-01 11:20:43.830  INFO 24150 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
2020-02-01 11:20:43.830  INFO 24150 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-2 - Shutdown completed.

> Task :security:test
2020-02-01 11:20:54.941  INFO 24188 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-02-01 11:20:54.944  INFO 24188 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-02-01 11:20:54.952  INFO 24188 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.1.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 46s
57 actionable tasks: 54 executed, 3 up-to-date

作为参考,从start.spring.io使用gradle创建的应用程序在屏幕上不产生任何输出

./gradlew build

BUILD SUCCESSFUL in 779ms
5 actionable tasks: 5 up-to-date

而是将输出放置在 build/reports/

就我而言,我尚未对启动随附的日志记录配置进行任何更改。没有logback.xml,也没有对application.yml进行日志记录级别的更改。我期望gradle捕获系统故障和系统错误并将其发送到,build/reports/但是某些输出似乎正在逃逸到系统故障。


2
将这些软件包或类的日志记录级别调整为低于INFO(或完全删除)。
Kayaman

2
这些是INFO级别日志行。如您所见,它们源自关闭钩子,它们最终终止于配置日志记录的位置。我想从理论上讲,由于日志记录配置的更改以及钩子之后异步执行,消息最终可能会出现在预期的位置之外。因此,由于先前的配置已卸载,因此会将这些行默认添加到控制台。也许。
Kayaman

1
您可以添加测试类,也可以添加主应用程序类吗?以及与数据源配置关联的任何相关application.properties/yml吗?
达伦·福赛斯

3
当Gradle测试工作程序的输出重定向被取消后,关闭它们时,可能会发生关闭异常。这可能是值得讨论的话题。
eskatos

2
理想情况下,在您的测试中关闭Spring Boot,而不必依赖jvm shutdown挂钩,这将是spring问题。
eskatos

Answers:


4

@eskatos是正确的。在执行测试用例之后,在关闭工作进程之前,将日志管理器关闭。当工作进程关闭时,将执行所有关闭挂钩,并将它们重定向回控制台。

由于这些消息是由Spring Boot生成的,所以最好的方法是使用logback测试配置xml过滤关闭消息。

src / test / resources中的logback-test.xml之类的东西

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
            <evaluator> <!-- defaults to type ch.qos.logback.classic.boolex.JaninoEventEvaluator -->
                <expression>return event.getThreadName().contains("ShutdownHook");</expression>
            </evaluator>
            <OnMismatch>NEUTRAL</OnMismatch>
            <OnMatch>DENY</OnMatch>
        </filter>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="INFO">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

build.gradle

testCompile 'org.codehaus.janino:janino'

1
IMO迄今为止最好的解决方法。
Steffen Harbich

3

人们可以禁用与输出决定如何记录一个的任务,以控制测试JVM的标准输出/标准错误:TestLoggingContainer testLogging.showStandardStreams = false onOutputTest

apply plugin: 'java'

test {

    // show standard out and standard error of the test JVM on the console
    // can be used to disable the console output:
    testLogging.showStandardStreams = true

    // listen to standard out and standard error of the test JVM
    // can be used to make the logging optional:
    onOutput { descriptor, event ->
        logger.lifecycle("Test: " + descriptor + " produced standard out/err: " + event.message)
    }
}

这些流是TestLogEvent STANDARD_OUTSTANDARD_ERROR,它们来自JVM。当可以确定一个event.message包含时extShutdownHook,可以跳过日志记录。


看到您可以生成从start.spring.io一个项目,从这个问题start.spring.io/...重现该问题
AMS

它可能不是问题,因为它是INFOSpring的默认日志记录级别;可以设置其他日志级别,例如。logging.level.org.springframework=TRACE作为环境变量。
马丁·齐特勒

1
我相信关机钩子日志是在测试任务之外生成的。您能否更新答案以显示如何过滤关闭挂接消息?我认为最好过滤这些消息的地方是无论如何它们都是在春季启动时生成的。
Sagar Veeram

3

我可以通过将以下内容添加到src / test / resources中来隐藏特定于spring数据的测试日志(基于此spring-starter):application.properties

logging.level.root=ERROR

logging.level.org.springframework不会对com.zaxxer.hikari记录器产生影响,但是您在此处有灵活的选择

root=ERROR就像“大铁锤方法”)。

src/main/resources也是可能的,但不仅在测试时而且在应用程序运行时都有效)(application.properties仅是此属性的许多可能“位置”之一...另请参见:https : //docs.spring.io/spring-boot/ docs / current / reference / html / appendix-application-properties.html

有了这个,我得到了一个“无声的” gradle输出,同样在上clean build

$ ./gradlew clean build

BUILD SUCCESSFUL in 10s
7 actionable tasks: 7 executed

0

Gradle具有安静模式。

./gradlew build -q

但是您仍然需要有关测试的信息。您可以使用jacoco和sonarqube。它在这里这里都对我有用

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.