如何记录Spring 5 WebClient调用

Seb*_*Seb 25 java logging spring-boot spring-webflux

我正在尝试使用Spring 5 WebClient记录请求.你知道我怎么能实现这个目标吗?

(我使用的是Spring 5和Spring boot 2)

代码现在看起来像这样:

try {
    return webClient.get().uri(url, urlParams).exchange().flatMap(response -> response.bodyToMono(Test.class))
            .map(test -> xxx.set(test));
} catch (RestClientException e) {
    log.error("Cannot get counter from opus", e);
    throw e;
}
Run Code Online (Sandbox Code Playgroud)

Rus*_*nko 22

您可以使用ExchangeFilterFunction轻松完成

只需logRequest在创建WebClient使用时添加自定义过滤器WebClient.Builder.

以下是此类过滤器的示例以及如何将其添加到WebClient.

@Slf4j
@Component
public class MyClient {

    private final WebClient webClient;

    // Create WebClient instance using builder.
    // If you use spring-boot 2.0, the builder will be autoconfigured for you
    // with the "prototype" scope, meaning each injection point will receive
    // a newly cloned instance of the builder.
    public MyClient(WebClient.Builder webClientBuilder) {
        webClient = webClientBuilder // you can also just use WebClient.builder()
                .baseUrl("https://httpbin.org")
                .filter(logRequest()) // here is the magic
                .build();
    }

    // Just example of sending request
    public void send(String path) {
        ClientResponse clientResponse = webClient
                .get().uri(uriBuilder -> uriBuilder.path(path)
                        .queryParam("param", "value")
                        .build())
                .exchange()
                .block();
        log.info("Response: {}", clientResponse.toEntity(String.class).block());
    }

    // This method returns filter function which will log request data
    private static ExchangeFilterFunction logRequest() {
        return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
            log.info("Request: {} {}", clientRequest.method(), clientRequest.url());
            clientRequest.headers().forEach((name, values) -> values.forEach(value -> log.info("{}={}", name, value)));
            return Mono.just(clientRequest);
        });
    }

}
Run Code Online (Sandbox Code Playgroud)

然后只需要调用myClient.send("get");和记录消息就可以了.

输出示例:

Request: GET https://httpbin.org/get?param=value
header1=value1
header2=value2
Run Code Online (Sandbox Code Playgroud)

  • 如何从`clientRequest`获取请求体? (15认同)
  • 简短的回答:你不能。一旦您读取正文(记录它),消费者就无法再访问它。我认为,可以将主体流包装到某个缓冲流中来实现,但老实说我从未这样做过。而且它会消耗内存,使得反应式编程变得毫无意义。如果你确实需要记录主体,你可以让底层(Netty)来做到这一点。请参阅 [Matthew Buckett 的回答](/sf/answers/3905347281/) 来了解这个想法。 (4认同)
  • @PavanKumar这里的`block()`调用仅用于演示目的。无论如何,请求日志记录过滤器将起作用。要记录响应,您可以编写另一个[`ExchangeFilterFunction`](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/reactive/function/client/ExchangeFilterFunction.html# ofResponseProcessor-java.util.function.Function-)并记录响应。但是,在记录响应正文时要小心-由于它的流性质,没有包装就只能使用一次。 (3认同)

Abh*_*kar 22

您不一定需要滚动自己的记录器,reactor.ipc.netty.channel.ChannelOperationsHandler它是否适合您.只需将该类的日志记录系统配置为以DEBUG级别登录:

2017-11-23 12:52:04.562 DEBUG 41449 --- [ctor-http-nio-5] r.i.n.channel.ChannelOperationsHandler   : [id: 0x9183d6da, L:/127.0.0.1:57681 - R:localhost/127.0.0.1:8000] Writing object DefaultFullHttpRequest(decodeResult: success, version: HTTP/1.1, content: UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 0, widx: 0, cap: 0))
GET /api/v1/watch/namespaces/default/events HTTP/1.1
user-agent: ReactorNetty/0.7.1.RELEASE
host: localhost:8000
accept-encoding: gzip
Accept: application/json
content-length: 0
Run Code Online (Sandbox Code Playgroud)

减少错误的一种方法是尽可能不编写代码.

2018年11月:

随着spring-webflux:5.1.2.RELEASE,以上不再有效.请改用以下内容:

logging.level.org.springframework.web.reactive.function.client.ExchangeFunctions=DEBUG
...
2018-11-06 20:58:58.181 DEBUG 20300 --- [           main] o.s.w.r.f.client.ExchangeFunctions       : [2026fbff] HTTP GET http://localhost:8080/stocks/search?symbol=AAPL
2018-11-06 20:58:58.451 DEBUG 20300 --- [ctor-http-nio-4] o.s.w.r.f.client.ExchangeFunctions       : [2026fbff] Response 400 BAD_REQUEST
Run Code Online (Sandbox Code Playgroud)

要记录标题或表单正文,请将上面的内容设置为TRACElevel; 但是,这还不够:

ExchangeStrategies exchangeStrategies = ExchangeStrategies.withDefaults();
exchangeStrategies
    .messageWriters().stream()
    .filter(LoggingCodecSupport.class::isInstance)
    .forEach(writer -> ((LoggingCodecSupport)writer).setEnableLoggingRequestDetails(true));

client = WebClient.builder()
    .exchangeStrategies(exchangeStrategies)
Run Code Online (Sandbox Code Playgroud)


Sta*_*yuk 17

@Matthew Buckett 的回答向您展示了如何获取 Netty 线路日志记录。但是,格式不是很花哨(它包括十六进制转储)。但它可以通过扩展轻松定制io.netty.handler.logging.LoggingHandler

public class HttpLoggingHandler extends LoggingHandler {

    @Override
    protected String format(ChannelHandlerContext ctx, String event, Object arg) {
        if (arg instanceof ByteBuf) {
            ByteBuf msg = (ByteBuf) arg;
            return msg.toString(StandardCharsets.UTF_8);
        }
        return super.format(ctx, event, arg);
    }
}

Run Code Online (Sandbox Code Playgroud)

然后将其包含在您的WebClient配置中:

HttpClient httpClient = HttpClient.create()
    .tcpConfiguration(tcpClient ->
        tcpClient.bootstrap(bootstrap ->
            BootstrapHandlers.updateLogSupport(bootstrap, new HttpLoggingHandler())));

WebClient
    .builder()
    .clientConnector(new ReactorClientHttpConnector(httpClient))
    .build()
Run Code Online (Sandbox Code Playgroud)

例子:

webClient.post()
    .uri("https://postman-echo.com/post")
    .syncBody("{\"foo\" : \"bar\"}")
    .accept(MediaType.APPLICATION_JSON)
    .exchange()
    .block();
Run Code Online (Sandbox Code Playgroud)
2019-09-22 18:09:21.477 DEBUG   --- [ctor-http-nio-4] c.e.w.c.e.logging.HttpLoggingHandler     : [id: 0x505be2bb] REGISTERED
2019-09-22 18:09:21.489 DEBUG   --- [ctor-http-nio-4] c.e.w.c.e.logging.HttpLoggingHandler     : [id: 0x505be2bb] CONNECT: postman-echo.com/35.170.134.160:443
2019-09-22 18:09:21.701 DEBUG   --- [ctor-http-nio-4] c.e.w.c.e.logging.HttpLoggingHandler     : [id: 0x505be2bb, L:/192.168.100.35:55356 - R:postman-echo.com/35.170.134.160:443] ACTIVE
2019-09-22 18:09:21.836 DEBUG   --- [ctor-http-nio-4] c.e.w.c.e.logging.HttpLoggingHandler     : [id: 0x505be2bb, L:/192.168.100.35:55356 - R:postman-echo.com/35.170.134.160:443] READ COMPLETE
2019-09-22 18:09:21.905 DEBUG   --- [ctor-http-nio-4] c.e.w.c.e.logging.HttpLoggingHandler     : [id: 0x505be2bb, L:/192.168.100.35:55356 - R:postman-echo.com/35.170.134.160:443] READ COMPLETE
2019-09-22 18:09:22.036 DEBUG   --- [ctor-http-nio-4] c.e.w.c.e.logging.HttpLoggingHandler     : [id: 0x505be2bb, L:/192.168.100.35:55356 - R:postman-echo.com/35.170.134.160:443] USER_EVENT: SslHandshakeCompletionEvent(SUCCESS)
2019-09-22 18:09:22.082 DEBUG   --- [ctor-http-nio-4] c.e.w.c.e.logging.HttpLoggingHandler     : POST /post HTTP/1.1
user-agent: ReactorNetty/0.8.11.RELEASE
host: postman-echo.com
Accept: application/json
Content-Type: text/plain;charset=UTF-8
content-length: 15

{"foo" : "bar"}
2019-09-22 18:09:22.083 DEBUG   --- [ctor-http-nio-4] c.e.w.c.e.logging.HttpLoggingHandler     : [id: 0x505be2bb, L:/192.168.100.35:55356 - R:postman-echo.com/35.170.134.160:443] FLUSH
2019-09-22 18:09:22.086 DEBUG   --- [ctor-http-nio-4] c.e.w.c.e.logging.HttpLoggingHandler     : [id: 0x505be2bb, L:/192.168.100.35:55356 - R:postman-echo.com/35.170.134.160:443] READ COMPLETE
2019-09-22 18:09:22.217 DEBUG   --- [ctor-http-nio-4] c.e.w.c.e.logging.HttpLoggingHandler     : HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Date: Sun, 22 Sep 2019 15:09:22 GMT
ETag: W/"151-Llbe8OYGC3GeZCxttuAH3BOYBKA"
Server: nginx
set-cookie: sails.sid=s%3APe39li6V8TL8FOJOzSINZRkQlZ7HFAYi.UkLZjfajJqkq9fUfF2Y8N4JOInHNW5t1XACu3fhQYSc; Path=/; HttpOnly
Vary: Accept-Encoding
Content-Length: 337
Connection: keep-alive

{"args":{},"data":"{\"foo\" : \"bar\"}","files":{},"form":{},"headers":{"x-forwarded-proto":"https","host":"postman-echo.com","content-length":"15","accept":"application/json","content-type":"text/plain;charset=UTF-8","user-agent":"ReactorNetty/0.8.11.RELEASE","x-forwarded-port":"443"},"json":null,"url":"https://postman-echo.com/post"}
2019-09-22 18:09:22.243 DEBUG   --- [ctor-http-nio-4] c.e.w.c.e.logging.HttpLoggingHandler     : [id: 0x505be2bb, L:/192.168.100.35:55356 - R:postman-echo.com/35.170.134.160:443] READ COMPLETE
Run Code Online (Sandbox Code Playgroud)

如果你想抑制无用的(对你来说)日志条目,如(最后注意ACTIVE):

2019-09-22 18:09:21.701 DEBUG   --- [ctor-http-nio-4] c.e.w.c.e.logging.HttpLoggingHandler     : [id: 0x505be2bb, L:/192.168.100.35:55356 - R:postman-echo.com/35.170.134.160:443] ACTIVE
Run Code Online (Sandbox Code Playgroud)

您可以channelActive像这样覆盖和其他人:

@Override
public void channelActive(ChannelHandlerContext ctx) {
    ctx.fireChannelActive();
}
Run Code Online (Sandbox Code Playgroud)

答案基于https://www.baeldung.com/spring-log-webclient-calls

  • 添加链接 https://www.baeldung.com/spring-log-webclient-calls 中提到的构造函数后即可工作 (3认同)
  • tcpClient.bootstrap 已弃用,并且不清楚使用哪种方法 (3认同)

Ser*_*kov 13

Spring Boot 2.2.4 和 Spring 5.2.3 的 2020 年 2 月更新:

我没有设法完成spring.http.log-request-details=true它的工作,当前的Spring WebFlux 参考建议需要完成一些编码才能记录标头,尽管代码示例使用了不推荐使用的exchangeStrategies()方法。

仍然有一个被弃用的方法的替代品,因此用于在 WebClient 级别获取标头的紧凑代码片段可能如下所示:

WebClient webClient = WebClient.builder()
    .codecs(configurer -> configurer.defaultCodecs().enableLoggingRequestDetails(true))
    .build();
Run Code Online (Sandbox Code Playgroud)

随着进一步

logging.level.org.springframework.web.reactive.function.client.ExchangeFunctions=TRACE
Run Code Online (Sandbox Code Playgroud)

应该注意的是,并非所有的头都在 WebFluxExchangeFunctions级别可用(确实存在),因此根据@Matthew的建议HttpClient,在 Netty级别进行更多日志记录也可能是必不可少

WebClient webClient = WebClient.builder()
    .clientConnector(new ReactorClientHttpConnector(
        HttpClient.create()
            .wiretap(true)))
    .build()
Run Code Online (Sandbox Code Playgroud)

随着进一步

logging.level.reactor.netty.http.client.HttpClient: DEBUG
Run Code Online (Sandbox Code Playgroud)

这也会记录身体。


Fle*_*tch 12

如果您不想记录身体,那真的很容易。

春季启动> = 2.1.0

将以下内容添加到application.properties:

logging.level.org.springframework.web.reactive.function.client.ExchangeFunctions=TRACE
spring.http.log-request-details=true
Run Code Online (Sandbox Code Playgroud)

第二行使标头包含在日志中。

Spring Boot <2.1.0

将以下内容添加到application.properties:

logging.level.org.springframework.web.reactive.function.client.ExchangeFunctions=TRACE
Run Code Online (Sandbox Code Playgroud)

而不是上面的第二行,您需要声明一个这样的类:

@Configuration
static class LoggingCodecConfig {

    @Bean
    @Order(0)
    public CodecCustomizer loggingCodecCustomizer() {
        return (configurer) -> configurer.defaultCodecs()
                .enableLoggingRequestDetails(true);
    }

}
Run Code Online (Sandbox Code Playgroud)

Brian Clozel回答

  • `spring.http.log-request-details=true` 已弃用,取而代之的是 `spring.mvc.log-request-details=true` (4认同)
  • 好的,有时我应该搜索更长的解决方案:https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-logging-sensitive-data。这解释了为什么它打印蒙版以及如何禁用它。 (3认同)

Vla*_*mir 12

在 Spring Boot 2.4.0 中,HttpClient 的 wiretap() 方法具有额外的参数,您可以传递这些参数以以正常的人类可读格式显示完整的请求/响应标头和正文。使用格式 (AdvancedByteBufFormat.TEXTUAL)。

HttpClient httpClient = HttpClient.create()
      .wiretap(this.getClass().getCanonicalName(), LogLevel.DEBUG, AdvancedByteBufFormat.TEXTUAL);
ClientHttpConnector conn = new ReactorClientHttpConnector(httpClient);   

WebClient client =  WebClient.builder()
            .clientConnector(conn)
            .build();
Run Code Online (Sandbox Code Playgroud)

结果:

POST /score HTTP/1.1
Host: localhost:8080
User-Agent: insomnia/2020.5.2
Content-Type: application/json
access_: 
Authorization: Bearer eyJ0e....
Accept: application/json
content-length: 4506

WRITE: 4506B {"body":{"invocations":[{"id":....


READ: 2048B HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 2271
Server: Werkzeug/1.0.1 Python/3.7.7
Date: Fri, 29 Jan 2021 18:49:53 GMT

{"body":{"results":[.....
Run Code Online (Sandbox Code Playgroud)

  • 我们有一个新的赢家!这很简单并且按预期工作,谢谢! (6认同)
  • 这就是问题的答案。 (2认同)

Mat*_*ett 9

您可以要求请求进行窃听,从而让netty记录请求/响应,如果您这样创建Spring WebClient,则它将启用窃听选项。

        WebClient webClient = WebClient.builder()
            .clientConnector(new ReactorClientHttpConnector(
                HttpClient.create().wiretap(true)
            ))
            .build()
Run Code Online (Sandbox Code Playgroud)

然后进行日志记录设置:

logging.level.reactor.netty.http.client.HttpClient: DEBUG
Run Code Online (Sandbox Code Playgroud)

这将记录请求/响应的所有内容(包括主体),但是格式不是特定于HTTP的,因此可读性很差。

  • 但您仍然可以使用wiretap(HTTP_CLIENT, LogLevel.DEBUG, AdvancedByteBufFormat.TEXTUAL)代替wiretap(true) (9认同)
  • 非常感谢!尽管格式不是很易读,但它是我目前发现的唯一一种查看实际请求和响应主体通过线路的方式。 (3认同)

小智 9

有一种方法可以仅使用ExchangeFilterFunction来记录请求和响应正文。它独立于底层ClientHttpConnector,支持定制输出。实际输出不包含在实现中。相反,可以访问请求和响应正文的行包含解释性注释。将以下类实例添加到WebClient过滤器列表中:

import org.reactivestreams.Publisher;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.client.reactive.ClientHttpRequest;
import org.springframework.http.client.reactive.ClientHttpRequestDecorator;
import org.springframework.web.reactive.function.BodyInserter;
import org.springframework.web.reactive.function.client.ClientRequest;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import org.springframework.web.reactive.function.client.ExchangeFunction;
import reactor.core.publisher.BaseSubscriber;
import reactor.core.publisher.Mono;

import java.util.concurrent.atomic.AtomicBoolean;

public class LoggingExchangeFilterFunction implements ExchangeFilterFunction {

    @Override
    public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next) {
        BodyInserter<?, ? super ClientHttpRequest> originalBodyInserter = request.body();
        ClientRequest loggingClientRequest = ClientRequest.from(request)
                .body((outputMessage, context) -> {
                    ClientHttpRequestDecorator loggingOutputMessage = new ClientHttpRequestDecorator(outputMessage) {

                        private final AtomicBoolean alreadyLogged = new AtomicBoolean(false); // Not sure if thread-safe is needed...

                        @Override
                        public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
                            boolean needToLog = alreadyLogged.compareAndSet(false, true);
                            if (needToLog) {
                                // use `body.toString(Charset.defaultCharset())` to obtain request body
                            }
                            return super.writeWith(body);
                        }

                        @Override
                        public Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body) {
                            boolean needToLog = alreadyLogged.compareAndSet(false, true);
                            if (needToLog) {
                                BaseSubscriber<Publisher<? extends DataBuffer>> bodySubscriber = new BaseSubscriber<Publisher<? extends DataBuffer>>() {
                                    @Override
                                    protected void hookOnNext(Publisher<? extends DataBuffer> next) {
                                        // use `next.toString(Charset.defaultCharset())` to obtain request body element
                                    }
                                };
                                body.subscribe(bodySubscriber);
                                bodySubscriber.request(Long.MAX_VALUE);
                            }
                            return super.writeAndFlushWith(body);
                        }

                        @Override
                        public Mono<Void> setComplete() { // This is for requests with no body (e.g. GET).
                            boolean needToLog = alreadyLogged.compareAndSet(false, true);
                            if (needToLog) {
                                // A request with no body, could log `request.method()` and `request.url()`.
                            }
                            return super.setComplete();
                        }
                    };
                    return originalBodyInserter.insert(loggingOutputMessage, context);
                })
                .build();
        return next.exchange(loggingClientRequest)
                .map(
                        clientResponse -> clientResponse.mutate()
                                .body(f -> f.map(dataBuffer -> {
                                    // Use `dataBuffer.toString(Charset.defaultCharset())` to obtain response body.
                                    return dataBuffer;
                                }))
                                .build()
                );
    }

}
Run Code Online (Sandbox Code Playgroud)


Dan*_*ich 8

剧透:到目前为止,自定义日志记录ExchangeFilterFunction不支持记录正文。

就我而言,最好的日志记录是通过 Bealdung 的解决方案实现的(请参阅)。

因此,我设置了一个默认构建器,以便不同的 API 共享它。

@Bean
public WebClient.Builder defaultWebClient() {
    final var builder = WebClient.builder();
    if (LOG.isDebugEnabled()) {
        builder.clientConnector(new ReactorClientHttpConnector(
                HttpClient.create().wiretap("reactor.netty.http.client.HttpClient",
                        LogLevel.DEBUG, AdvancedByteBufFormat.TEXTUAL)
        ));
    }
    return builder;
}
Run Code Online (Sandbox Code Playgroud)

在具体的 API 配置中,我可以配置特定的东西:

@Bean
public SpecificApi bspApi(@Value("${specific.api.url}") final String baseUrl,
                     final WebClient.Builder builder) {
    final var webClient = builder.baseUrl(baseUrl).build();
    return new SpecificApi(webClient);
}
Run Code Online (Sandbox Code Playgroud)

然后我必须设置以下属性:

logging.level.reactor.netty.http.client: DEBUG
Run Code Online (Sandbox Code Playgroud)

然后请求日志如下所示:

021-03-03 12:56:34.589 DEBUG 20464 --- [ctor-http-nio-2] reactor.netty.http.client.HttpClient     : [id: 0xe75a7fb8] REGISTERED
2021-03-03 12:56:34.590 DEBUG 20464 --- [ctor-http-nio-2] reactor.netty.http.client.HttpClient     : [id: 0xe75a7fb8] CONNECT: /192.168.01:80
2021-03-03 12:56:34.591 DEBUG 20464 --- [ctor-http-nio-2] reactor.netty.http.client.HttpClient     : [id: 0xe75a7fb8, L:/192.168.04:56774 - R:/192.168.01:80] ACTIVE
2021-03-03 12:56:34.591 DEBUG 20464 --- [ctor-http-nio-2] r.netty.http.client.HttpClientConnect    : [id: 0xe75a7fb8, L:/192.168.04:56774 - R:/192.168.01:80] Handler is being applied: {uri=http://192.168.01/user, method=GET}
2021-03-03 12:56:34.592 DEBUG 20464 --- [ctor-http-nio-2] reactor.netty.http.client.HttpClient     : [id: 0xe75a7fb8, L:/192.168.04:56774 - R:/192.168.01:80] WRITE: 102B GET /user HTTP/1.1
user-agent: ReactorNetty/1.0.3
host: 192.168.01
accept: */*

<REQUEST_BODY>

2021-03-03 12:56:34.592 DEBUG 20464 --- [ctor-http-nio-2] reactor.netty.http.client.HttpClient     : [id: 0xe75a7fb8, L:/192.168.04:56774 - R:/192.168.01:80] FLUSH
2021-03-03 12:56:34.592 DEBUG 20464 --- [ctor-http-nio-2] reactor.netty.http.client.HttpClient     : [id: 0xe75a7fb8, L:/192.168.04:56774 - R:/192.168.01:80] WRITE: 0B 
2021-03-03 12:56:34.592 DEBUG 20464 --- [ctor-http-nio-2] reactor.netty.http.client.HttpClient     : [id: 0xe75a7fb8, L:/192.168.04:56774 - R:/192.168.01:80] FLUSH
2021-03-03 12:56:34.594 DEBUG 20464 --- [ctor-http-nio-2] reactor.netty.http.client.HttpClient     : [id: 0xe75a7fb8, L:/192.168.04:56774 - R:/192.168.01:80] READ: 2048B HTTP/1.1 200 
Server: nginx/1.16.1
Date: Wed, 03 Mar 2021 11:56:31 GMT
Content-Type: application/json
Content-Length: 4883
Connection: keep-alive
Access-Control-Allow-Origin: *
Content-Range: items 0-4/4

<RESPONSE_BODY>
Run Code Online (Sandbox Code Playgroud)


Ste*_*erl 8

当涉及 Spring 的响应式 WebClient 时,正确记录请求/响应日志确实很困难。

我有以下要求:

  • 记录请求和响应,包括一条日志语句中的正文(如果您在 AWS cloudwatch 中滚动浏览数百条日志,则将所有内容都包含在一条语句中要方便得多)
  • 从日志中过滤个人数据或财务数据等敏感数据,以符合 GDPR 和 PCI 的要求

因此窃听 Netty或使用自定义 Jackson 编码/解码器不是一个选择。

这是我对这个问题的看法(再次基于斯坦尼斯拉夫的出色回答)。

(下面的代码使用了Lombok注释处理,如果你还没有使用它,你可能也想使用它。否则应该很容易去lombok)

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.reactivestreams.Publisher;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.client.reactive.ClientHttpRequest;
import org.springframework.http.client.reactive.ClientHttpRequestDecorator;
import org.springframework.lang.NonNull;
import org.springframework.util.StopWatch;
import org.springframework.web.reactive.function.BodyInserter;
import org.springframework.web.reactive.function.client.ClientRequest;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import org.springframework.web.reactive.function.client.ExchangeFunction;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.concurrent.atomic.AtomicBoolean;

import static java.lang.Math.min;
import static java.util.UUID.randomUUID;
import static net.logstash.logback.argument.StructuredArguments.v;

@Slf4j
@RequiredArgsConstructor
public class RequestLoggingFilterFunction implements ExchangeFilterFunction {

  private static final int MAX_BYTES_LOGGED = 4_096;

  private final String externalSystem;

  @Override
  @NonNull
  public Mono<ClientResponse> filter(@NonNull ClientRequest request, @NonNull ExchangeFunction next) {
    if (!log.isDebugEnabled()) {
      return next.exchange(request);
    }

    var clientRequestId = randomUUID().toString();

    var requestLogged = new AtomicBoolean(false);
    var responseLogged = new AtomicBoolean(false);

    var capturedRequestBody = new StringBuilder();
    var capturedResponseBody = new StringBuilder();

    var stopWatch = new StopWatch();
    stopWatch.start();

    return next
      .exchange(ClientRequest.from(request).body(new BodyInserter<>() {

        @Override
        @NonNull
        public Mono<Void> insert(@NonNull ClientHttpRequest req, @NonNull Context context) {
          return request.body().insert(new ClientHttpRequestDecorator(req) {

            @Override
            @NonNull
            public Mono<Void> writeWith(@NonNull Publisher<? extends DataBuffer> body) {
              return super.writeWith(Flux.from(body).doOnNext(data -> capturedRequestBody.append(extractBytes(data)))); // number of bytes appended is maxed in real code
            }

          }, context);
        }
      }).build())
      .doOnNext(response -> {
          if (!requestLogged.getAndSet(true)) {
            log.debug("| >>---> Outgoing {} request [{}]\n{} {}\n{}\n\n{}\n",
              v("externalSystem", externalSystem),
              v("clientRequestId", clientRequestId),
              v("clientRequestMethod", request.method()),
              v("clientRequestUrl", request.url()),
              v("clientRequestHeaders", request.headers()), // filtered in real code
              v("clientRequestBody", capturedRequestBody.toString()) // filtered in real code
            );
          }
        }
      )
      .doOnError(error -> {
        if (!requestLogged.getAndSet(true)) {
          log.debug("| >>---> Outgoing {} request [{}]\n{} {}\n{}\n\nError: {}\n",
            v("externalSystem", externalSystem),
            v("clientRequestId", clientRequestId),
            v("clientRequestMethod", request.method()),
            v("clientRequestUrl", request.url()),
            v("clientRequestHeaders", request.headers()), // filtered in real code
            error.getMessage()
          );
        }
      })
      .map(response -> response.mutate().body(transformer -> transformer
          .doOnNext(body -> capturedResponseBody.append(extractBytes(body))) // number of bytes appended is maxed in real code
          .doOnTerminate(() -> {
            if (stopWatch.isRunning()) {
              stopWatch.stop();
            }
          })
          .doOnComplete(() -> {
            if (!responseLogged.getAndSet(true)) {
              log.debug("| <---<< Response for outgoing {} request [{}] after {}ms\n{} {}\n{}\n\n{}\n",
                v("externalSystem", externalSystem),
                v("clientRequestId", clientRequestId),
                v("clientRequestExecutionTimeInMillis", stopWatch.getTotalTimeMillis()),
                v("clientResponseStatusCode", response.statusCode().value()),
                v("clientResponseStatusPhrase", response.statusCode().getReasonPhrase()),
                v("clientResponseHeaders", response.headers().asHttpHeaders(), // HttpHeaders implement toString() with formatting, filtered in real code
                v("clientResponseBody", capturedResponseBody.toString()) // filtered in real code
              );
            }
          })
          .doOnError(error -> {
              if (!responseLogged.getAndSet(true)) {
                log.debug("| <---<< Error parsing response for outgoing {} request [{}] after {}ms\n{}",
                  v("externalSystem", externalSystem),
                  v("clientRequestId", clientRequestId),
                  v("clientRequestExecutionTimeInMillis", stopWatch.getTotalTimeMillis()),
                  v("clientErrorMessage", error.getMessage())
                );
              }
            }
          )
        ).build()
      );
  }

  private static String extractBytes(DataBuffer data) {
    int currentReadPosition = data.readPosition();
    var numberOfBytesLogged = min(data.readableByteCount(), MAX_BYTES_LOGGED);
    var bytes = new byte[numberOfBytesLogged];
    data.read(bytes, 0, numberOfBytesLogged);
    data.readPosition(currentReadPosition);
    return new String(bytes);
  }

}
Run Code Online (Sandbox Code Playgroud)

成功交换的日志条目如下所示:

2021-12-07 17:14:04.029 DEBUG --- [ctor-http-nio-3] RequestLoggingFilterFunction        : | >>---> Outgoing SnakeOil request [6abd0170-d682-4ca6-806c-bbb3559998e8]
POST https://localhost:8101/snake-oil/oauth/token
Content-Type: application/x-www-form-urlencoded

grant_type=client_credentials&client_id=*****&client_secret=*****
Run Code Online (Sandbox Code Playgroud)
2021-12-07 17:14:04.037 DEBUG --- [ctor-http-nio-3] RequestLoggingFilterFunction        : | <---<< Response for outgoing SnakeOil request [6abd0170-d682-4ca6-806c-bbb3559998e8] after 126ms
200 OK
Content-Type: application/json
Vary: [Accept-Encoding, User-Agent]
Transfer-Encoding: chunked

{"access_token":"*****","expires_in":"3600","token_type":"BearerToken"}
Run Code Online (Sandbox Code Playgroud)

当然,错误情况也会得到妥善处理。


mat*_*rns 7

这就是 2021 年对我有用的:)

HttpClient httpClient = HttpClient
        .create()
        .wiretap(this.getClass().getCanonicalName(),
                LogLevel.INFO, AdvancedByteBufFormat.TEXTUAL);

WebClient client = WebClient.builder()
        .baseUrl("https://example.com")
        .clientConnector(new ReactorClientHttpConnector(httpClient))
        .build();
Run Code Online (Sandbox Code Playgroud)