如何从这些单元测试中捕获Flux的日志输出?

huangapple go评论70阅读模式
英文:

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(&quot;Starting&quot;);
Flux.range(0, 5)
.doOnNext(val -&gt; log.info(&quot;Emitted {}&quot;, val))
.delayElements(Duration.of(2, ChronoUnit.SECONDS))
.subscribe(
tick -&gt; log.info(&quot;Tick {}&quot;, tick),
(ex) -&gt; log.info(&quot;Error emitted&quot;),
() -&gt; {
log.info(&quot;Completed&quot;);
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

+        &lt;plugin&gt;
+          &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
+          &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
+          &lt;configuration&gt;
+            &lt;redirectTestOutputToFile&gt;true&lt;/redirectTestOutputToFile&gt;
+          &lt;/configuration&gt;
+        &lt;/plugin&gt;

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] ------------------&lt; com.balamaci:reactor-playground &gt;-------------------
[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:

&lt;dependency&gt;
&lt;groupId&gt;ch.qos.logback&lt;/groupId&gt;
&lt;artifactId&gt;logback-classic&lt;/artifactId&gt;
&lt;version&gt;1.2.3&lt;/version&gt;
&lt;/dependency&gt;

and create resources/logback.xml

&lt;configuration&gt;
&lt;appender name=&quot;STDOUT&quot; class=&quot;ch.qos.logback.core.ConsoleAppender&quot;&gt;
&lt;!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --&gt;
&lt;encoder&gt;
&lt;pattern&gt;%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n&lt;/pattern&gt;
&lt;/encoder&gt;
&lt;/appender&gt;
&lt;root level=&quot;info&quot; additivity=&quot;false&quot;&gt;
&lt;appender-ref ref=&quot;STDOUT&quot;/&gt;
&lt;/root&gt;
&lt;/configuration&gt;

答案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.

huangapple
  • 本文由 发表于 2020年8月21日 10:52:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63515838.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定