英文:
How to capture log output from Flux in these unit tests?
问题
我正在查看此项目中的示例代码:https://github.com/balamaci/reactor-core-playground#simple-operators
以下是源代码中的一个典型函数:
@Test
public void delayElements() {
CountDownLatch latch = new CountDownLatch(1);
log.info("Starting");
Flux.range(0, 5)
.doOnNext(val -> log.info("Emitted {}", val))
.delayElements(Duration.of(2, ChronoUnit.SECONDS))
.subscribe(
tick -> log.info("Tick {}", tick),
(ex) -> log.info("Error emitted"),
() -> {
log.info("Completed");
latch.countDown();
});
Helpers.wait(latch);
}
我希望能够看到类似于“Error emitted”的日志输出。
然而,如果我运行“mvn test”,我根本看不到任何标准输出。
我尝试过在surefire插件中启用日志捕获,但也没有任何效果:
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <redirectTestOutputToFile>true</redirectTestOutputToFile>
+ </configuration>
+ </plugin>
资源中有一个属性文件:
org.slf4j.simpleLogger.logFile=System.out
org.slf4j.simpleLogger.defaultLogLevel=info
org.slf4j.simpleLogger.showDateTime=true
org.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss:SSS
org.slf4j.simpleLogger.showThreadName=true
org.slf4j.simpleLogger.showShortLogName=true
但输出仍然是这样的:
$ mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.balamaci:reactor-playground >-------------------
[INFO] Building Reactor-Core Playground 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
...
[INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ reactor-playground ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
...
在这种情况下,我如何捕获日志输出?
英文:
I am checking out the example code in this project https://github.com/balamaci/reactor-core-playground#simple-operators
Here is a typical function in the source code
@Test
public void delayElements() {
CountDownLatch latch = new CountDownLatch(1);
log.info("Starting");
Flux.range(0, 5)
.doOnNext(val -> log.info("Emitted {}", val))
.delayElements(Duration.of(2, ChronoUnit.SECONDS))
.subscribe(
tick -> log.info("Tick {}", tick),
(ex) -> log.info("Error emitted"),
() -> {
log.info("Completed");
latch.countDown();
});
Helpers.wait(latch);
}
I want to be able to see the log output like Error emitted
.
However if i run mvn test
I cannot see any stdout output at all.
I have tried to enable log capture in the surefire plugin but it also does not achieve anything
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <redirectTestOutputToFile>true</redirectTestOutputToFile>
+ </configuration>
+ </plugin>
There is a property file in the resource
org.slf4j.simpleLogger.logFile=System.out
org.slf4j.simpleLogger.defaultLogLevel=info
org.slf4j.simpleLogger.showDateTime=true
org.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss:SSS
org.slf4j.simpleLogger.showThreadName=true
org.slf4j.simpleLogger.showShortLogName=true
But the output is still
$ mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< com.balamaci:reactor-playground >-------------------
[INFO] Building Reactor-Core Playground 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ reactor-playground ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/antkong/experiments/flux/reactor-core-playground/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ reactor-playground ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ reactor-playground ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ reactor-playground ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ reactor-playground ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.657 s
[INFO] Finished at: 2020-08-21T18:27:58+10:00
[INFO] ------------------------------------------------------------------------
In this case, how can I capture the log output?
答案1
得分: 1
好的,以下是您要翻译的内容:
似乎您的类路径上没有任何记录器实现。尝试添加以下内容:
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
并创建 resources/logback.xml
文件:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- 编码器默认情况下分配类型为 ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="info" additivity="false">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
英文:
It seems like you don’t have any logger implementation on classpath. Try adding the following:
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
and create resources/logback.xml
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="info" additivity="false">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
答案2
得分: 1
这里的问题似乎是类名中缺少了 Test
一词,因此它们根本没有被执行。像 mvn test -Dtest=Part02SimpleOperators
这样的命令,在其中提供一个名称,可以正常运行。
英文:
The issue here seems to be lack of Test
word in class names, so they are not ran at all. Commands like mvn test -Dtest=Part02SimpleOperators
, where you provide a name, work just fine.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论