【问题标题】:Spring Webflux, log response body from reactive http request only in debug modeSpring Webflux,仅在调试模式下记录来自反应式 http 请求的响应正文
【发布时间】:2021-05-20 12:54:19
【问题描述】:

关于如何从 Webflux Webclient 发送的 HTTP 请求中记录响应正文的小问题,但只能在调试模式下

目前,在应用程序中,我正在以这种方式进行响应式 http 出站调用:

webClient.mutate()
         .baseUrl(someURL)
         .build()
         .post()
         .uri(someRoute)
         .body(BodyInserters.fromValue(myRequest))
         .retrieve()
         .bodyToMono(String.class);

这工作正常。 但是,有时会出现问题,我希望查看响应正文以进行调试。

因此,我做出了错误的决定来使用 .log()

webClient.mutate()
         .baseUrl(someURL)
         .build()
         .post()
         .uri(someRoute)
         .body(BodyInserters.fromValue(myRequest))
         .retrieve()
         .bodyToMono(String.class)
         .log();

这很棒,我可以看到一些日志,类似于这样:

[ctor-http-nio-3] reactor.Mono.LiftFuseable.1              : | onSubscribe([Fuseable] ScopePassingSpanSubscriber)
[ctor-http-nio-3] reactor.Mono.LiftFuseable.1              : | request(unbounded)
[ctor-http-nio-3] reactor.Mono.LiftFuseable.1              : | onNext({foo='bar'})
[ctor-http-nio-3] reactor.Mono.LiftFuseable.1              : | onComplete()

但是,无论日志级别如何,我现在都在记录这个...

无论我使用logging.level.root=DEBUG 设置的级别如何,我现在总是能看到日志。

请问如何实现这一点,但仅限调试日志级别吗?我正在查看 API,似乎 .log() 可以带参数,但我无法使其仅用于调试。

或者也许其他一些仅在调试级别实现记录响应正文的方法会很棒。

谢谢

【问题讨论】:

    标签: java spring-webflux


    【解决方案1】:

    您可以根据日志记录级别(或其他一些配置文件/环境指标)有条件地添加 log()

    
    Mono<String> makeCall(final WebClient webClient, final ?? myRequest) { 
       final Mono<String> response = webClient.mutate()
                                              .baseUrl(someURL)
                                              .build()
                                              .post()
                                              .uri(someRoute)
                                              .body(BodyInserters.fromValue(myRequest))
                                              .retrieve()
                                              .bodyToMono(String.class);
    
        if (LOG.isDebugEnabled()) {
            return response.log();
        }
        return response;
    }
    
    

    【讨论】:

      猜你喜欢
      • 2017-12-27
      • 2019-08-21
      • 2019-10-02
      • 2018-04-21
      • 1970-01-01
      • 2017-12-18
      • 2018-02-08
      • 2020-03-24
      • 1970-01-01
      相关资源
      最近更新 更多