使用Spring WebClient读取HTTP请求的可读调试日志 [英] Readable debug logging for http requests with spring webclient

查看:161
本文介绍了使用Spring WebClient读取HTTP请求的可读调试日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring响应式WebClient向HTTP服务器发送请求.为了查看基础请求&发送的响应中,我为reactor.ipc.netty程序包启用了调试日志记录.

I'm using Spring reactive WebClient for sending requests to a http server. Inorder to view the underlying request & response that's being sent, I enabled debug logging for reactor.ipc.netty package.

可以正常查看传出请求的标头.

The headers for the outgoing requests can be viewed normally.

我要发送的&通过http接收纯文本,日志中包含请求&以下格式的回复(是十六进制的吗?)

Tho I'm sending & receiving plain text over http, the log contains the request & responses in the below format (is it hex?)

我不确定如何以一种易于理解的方式查看记录的数据.更好的是记录请求&可以理解的方式回应

I'm not sure how to view the logged data in a easy to understand way. Better yet log the request & response in a understandable way

这是已记录数据的一个片段

Here is a snippet of the logged data

         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 47 45 54 20 2f 53 65 61 72 63 68 5f 47 43 2e 61 |GET /Search_GC.a|
|00000010| 73 70 78 20 48 54 54 50 2f 31 2e 31 0d 0a 75 73 |spx HTTP/1.1..us|
|00000020| 65 72 2d 61 67 65 6e 74 3a 20 52 65 61 63 74 6f |er-agent: Reacto|
|00000030| 72 4e 65 74 74 79 2f 30 2e 37 2e 32 2e 52 45 4c |rNetty/0.7.2.REL|
|00000040| 45 41 53 45 0d 0a 68 6f 73 74 3a 20 63 65 6f 6b |EASE..host: ceok|
|00000050| 61 72 6e 61 74 61 6b 61 2e 6b 61 72 2e 6e 69 63 |arnataka.kar.nic|
|00000060| 2e 69 6e 0d 0a 61 63 63 65 70 74 3a 20 2a 2f 2a |.in..accept: */*|
|00000070| 0d 0a 61 63 63 65 70 74 2d 65 6e 63 6f 64 69 6e |..accept-encodin|
|00000080| 67 3a 20 67 7a 69 70 0d 0a 63 6f 6e 74 65 6e 74 |g: gzip..content|
|00000090| 2d 6c 65 6e 67 74 68 3a 20 30 0d 0a 0d 0a       |-length: 0....  |
+--------+-------------------------------------------------+----------------+

找到了一个由于相同的库而必须发生的未回答的问题:

Found an unanswered question that must be happening because of the same library: Reading a HttpContent that has a PooledUnsafeDirectByteBuf

此处

在传统的观点看来,反应式客户端不需要调试.当我们使用rest clientpostmancurlhttpie&其他人发送请求并查看响应

There seems to an orthodox view that debugging is not required for reactive clients. This is a completely pointless arguments as we use tools like rest client, postman, curl, httpie & others to send request and view response

推荐答案

如果您将DataBuffer用作阅读器,则可以使用doOnNext()来做到这一点:

You can do it with doOnNext(), if you use DataBuffer as your reader:

public Mono<ServerResponse> selectByPost(ServerRequest request) {
  Flux<DataBuffer> requestBodyFlux = request.bodyToFlux(DataBuffer.class)
    .doOnNext(dataBuffer -> {
      if (debug ) {
        log.debug(new String(dataBuffer.asByteBuffer().array()));
      }
      Scannable.from(dataBuffer).tags().forEach(System.out::println);
    });
}

这可能不是最好的方法,如果netty提供了记录有效负载的不同方法,那么这当然是一个不错的功能.十六进制确实有其优点,这取决于您需要调试什么.

This is probably not the best way to do it, it would of course be a nice feature, if netty would provide different ways of logging the payload. Hex does have its benefits, depending on what you need to debug.

这篇关于使用Spring WebClient读取HTTP请求的可读调试日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆