[英]how to log pipelines in Java project reactor?
I have started a new project that uses java reactor and spring webFLux.我已经启动了一个使用 java 反应器和 spring webFLux 的新项目。 Recently I had to debug a production bug and It was a nightmare since they do not log anything.
最近我不得不调试一个生产错误,这是一场噩梦,因为他们没有记录任何东西。 So, reading I found two ways to start adding logs to pipelines.
因此,通过阅读我发现了两种开始向管道添加日志的方法。 One by using.log() and another one by using onErrorResume, doOnSubscribe, doOnSuccess.
一种是使用 .log(),另一种是使用 onErrorResume、doOnSubscribe、doOnSuccess。 Do you know which one should I use?
你知道我应该使用哪一个吗? Are better ways to log pipelines?
是记录管道的更好方法吗?
return repositoryName.findById(event.eventId())
.filter(event -> event.completedDate() == null)
.filterWhen(event ->
externalService.getEventSummary(event.getUser().userId())
.doOnNext(event -> log.info("Event found {} and should be marked as found", event.id()))
I highly recommend the Reactor doc to check what each method does https://projectreactor.io/docs/core/release/reference/我强烈推荐 Reactor 文档来检查每个方法的作用https://projectreactor.io/docs/core/release/reference/
doOnNext , doOnError , doOnSuccess are highly used to execute blocking operations without side effects on the current stream. If you received a Square object on your doOnNext and doOnError , this method would return precisely the received Square object (even if you changed its properties) doOnNext 、 doOnError 、 doOnSuccess被广泛用于执行阻塞操作而不会对当前 stream 产生副作用。如果您在doOnNext和doOnError上收到一个 Square object ,此方法将准确返回收到的 Square object(即使您更改了它的属性)
doOnNext to execute after the previous sequence finishes try-catch doOnNext在前一个序列完成后执行try-catch
doOnError the subsequent execution if an exception was thrown (and there's no other exception handler on stream) doOnError如果抛出异常则后续执行(并且流上没有其他异常处理程序)
doOnSuccess is similar to a finally block from try-catch doOnSuccess类似于来自try-catch的 finally 块
onErrorResume is most used to handle exceptions if needed, similar to catch(Exception e), but it will return a new object onErrorResume最多用于在需要时处理异常,类似于 catch(Exception e),但它会返回一个新的 object
As you can see, it's okay to have logs on flatMap , map , doOnNext , doOnError , or onErrorResume .如您所见,可以在flatMap 、 map 、 doOnNext 、 doOnError或onErrorResume上记录日志。 The correct one to use will vary on what you need.
正确使用的会因您的需要而异。 If logging is the only thing you need, I recommend doOnNext , doOnError , or doOnFinally .
如果日志记录是您唯一需要的东西,我推荐doOnNext 、 doOnError或doOnFinally 。
As said before, please refer to the docs too to check what each method stands for如前所述,请也参考文档以检查每种方法代表什么
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.