Spring Webflux Webclient:窃听日志记录问题

问题描述 投票:0回答:2

有关 Java Spring Webflux WebClient 和窃听日志记录的快速问题。

我目前正在使用 Java 和 SpringBoot Webflux WebClient 2.5.0 发出出站 http 请求(我是源),我需要发出 http 请求(到目标)。

我像这样制作出站http客户端:

webClientUtil.getWebClient().mutate().baseUrl(someUrl).build().post().body(BodyInserters.fromValue(someRequest)).exchangeToMono(clientResponse -> clientResponse.bodyToMono(SomeResponse.class));

我想记录请求和响应,因此,我正在编写以下代码:

final HttpClient httpClient = HttpClient.create().wiretap("reactor.netty.http.client.HttpClient", LogLevel.INFO, AdvancedByteBufFormat.HEX_DUMP)

在 Util 类中:

public WebClient getWebClient() {
        final HttpClient httpClient = HttpClient.create().wiretap("reactor.netty.http.client.HttpClient", LogLevel.INFO, AdvancedByteBufFormat.HEX_DUMP).metrics(true, Function.identity()).proxy(proxy -> proxy.type(ProxyProvider.Proxy.HTTP).host(proxyUrl));
        return WebClient.create().mutate().defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE, HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE).clientConnector(new ReactorClientHttpConnector(httpClient)).build();
    }

(WebClient植入已测试并正常工作)

我将日志记录级别设置为 INFO,并使用

logging.level.root=INFO

请注意日志级别是info,窃听也是info。

不幸的是,我没有看到任何额外的日志。

请问我做错了什么?

谢谢!

java spring-webflux
2个回答
0
投票

任何 logback.xml 文件会覆盖配置?

我在一些测试中遇到了类似的问题,并通过在我的

logback-test.xml
中解决了它:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <include resource="org/springframework/boot/logging/logback/base.xml" />

  <logger name="reactor.netty" level="debug"/>

</configuration> 

0
投票

在Web客户端中,您需要将Log Level添加为DEBUG,而不是INFO

final HttpClient httpClient = HttpClient.create().wiretap("reactor.netty.http.client.HttpClient", LogLevel.DEBUG, AdvancedByteBufFormat.HEX_DUMP)

接下来,您需要为httpclient包设置日志级别

logging.level.reactor.netty.http.client=DEBUG

有了这个你应该能够看到日志

您可以参考这篇baeldung文章了解更多信息

© www.soinside.com 2019 - 2024. All rights reserved.