您可以从start.spring.io生成此问题的项目https://start.spring.io/starter.zip?type=gradle-项目
我有一个用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
作为参考,使用gradle start.spring.io 创建的应用程序不会在屏幕上产生输出
./gradlew build
BUILD SUCCESSFUL in 779ms
5 actionable tasks: 5 up-to-date
相反,输出放在< code>build/reports/中
在我的例子中,我没有对boot附带的日志配置进行任何更改。没有logback.xml,也没有对日志记录级别的application.yml进行更改。我希望gradle能够捕获系统输出和系统错误,并将其发送到< code>build/reports/中,但是有些输出似乎会逃逸到系统输出中。
@eskatos是对的。日志管理器在关闭工作进程之前执行测试用例后被拆除。所有关闭挂钩都在工作进程关闭时执行,并被重定向回控制台。
因为这些消息是由spring boot生成的,所以最好的方法是使用回退测试配置xml来过滤关闭消息。
logback-test.xml src/test/资源
<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>
建筑物玻璃
testCompile 'org.codehaus.janino:janino'
用户可以使用< code > TestLoggingContainer < code > Test logging . showstandardstreams = false 禁用输出,或者决定在< code>Test任务的< code>onOutput中记录什么,以便控制测试JVM的stdout/stderr:
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_OUT
我可以通过将以下 application.properties
添加到 src/test/resources 来隐藏特定于 spring-data 的测试日志记录(基于这个 spring-starter):
logging.level.root=ERROR
logging.level.org.springframework
不会对例如 com.zaxxer.hikari
logger 产生影响,但这里有灵活的选择。
(< code>root=ERROR类似于“大锤方法”)。
(src/main/resources
也是可能的,但不仅在测试中有效,而且在应用程序运行时也有效)(application.properties
只是此属性的众多可能“位置”之一...另请参阅:https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html)
有了这个,我得到了一个“静默”的gradle输出,也是在干净构建
上:
$ ./gradlew clean build
BUILD SUCCESSFUL in 10s
7 actionable tasks: 7 executed