小编nsp*_*ung的帖子

如何记录 spring-webflux WebClient 请求+响应详细信息(正文、标头、elasped_time)?

基本上,我想在一个包含 Spring 的主体/标头的日志中记录请求/响应信息WebClient

有了 Spring,RestTemplate我们就可以用ClientHttpRequestInterceptor. 我找到了ExchangeFilterFunctionSpring 的相关内容WebClient,但还没有设法以干净的方式做类似的事情。我们可以使用此过滤器并记录请求,然后记录响应,但我需要在同一日志跟踪上两者。

此外,我还没有设法用ExchangeFilterFunction.ofResponseProcessor方法获取响应正文。

我期望这样的日志(当前的实现与 ClientHttpRequestInterceptor 一起使用)包含我需要的所有信息:

{
    "@timestamp": "2019-05-14T07:11:29.089+00:00",
    "@version": "1",
    "message": "GET https://awebservice.com/api",
    "logger_name": "com.sample.config.resttemplate.LoggingRequestInterceptor",
    "thread_name": "http-nio-8080-exec-5",
    "level": "TRACE",
    "level_value": 5000,
    "traceId": "e65634ee6a7c92a7",
    "spanId": "7a4d2282dbaf7cd5",
    "spanExportable": "false",
    "X-Span-Export": "false",
    "X-B3-SpanId": "7a4d2282dbaf7cd5",
    "X-B3-ParentSpanId": "e65634ee6a7c92a7",
    "X-B3-TraceId": "e65634ee6a7c92a7",
    "parentId": "e65634ee6a7c92a7",
    "method": "GET",
    "uri": "https://awebservice.com/api",
    "body": "[Empty]",
    "elapsed_time": 959,
    "status_code": 200,
    "status_text": "OK",
    "content_type": "text/html",
    "response_body": "{"message": "Hello World!"}"
}
Run Code Online (Sandbox Code Playgroud)

有人设法用 Spring WebClient 做这样的事情吗?或者如何继续使用 Spring WebClient …

logging spring-boot spring-web spring-webflux spring-webclient

9
推荐指数
1
解决办法
2万
查看次数