0

I have a Spring Boot application, a reactive service which calls external systems via WebClient. I also have some non-reactive services. There is already implemented logging using MDC + Logbook for Feign clients. It's highly desirable to reuse existing classes or at least make log records look the same as non-reactive services logs.

RESPONSE_CODE=200, RESPONSE_HEADERS=[transfer-encoding:"chunked"], METHOD=POST, REQUEST_QUERY={}, REQUEST_BODY=FluxMap, RESPONSE_BODY=, REQUEST_PATH=/ping, REQUEST_HEADERS=[Session:"11111abcd", Content-Type:"application/json", ..., Content-Length:"37"], TYPE=INREQ

Current logs look like that. Problematic parts are REQUEST_BODY=FluxMap and RESPONSE_BODY=

I managed to log body using wiretap() on WebClient, but it's not customisable, so it doesn't suit.

For reference, I used sample code from this article which I found in this Github discussion

I've tried different approaches: using ExchangeFilterFunction and WebFilter + ServerHttpRequestDecorator (and other decorators) where I try to get body from DataBuffer. Still no luck.

stakeika
  • 55
  • 1
  • 8

1 Answers1

0

Solution that worked for me:

  1. Add this dependency
<dependency>
   <groupId>org.zalando</groupId>
   <artifactId>logbook-spring-boot-webflux-autoconfigure</artifactId>
</dependency>
  1. Pass Logbook object when you create a WebClient
private WebClient getWebClient(Logbook logbook) {
    HttpClient httpClient = HttpClient.create()
            .doOnConnected(connection -> {
                connection.addHandlerLast(new LogbookClientHandler(logbook));
            });

    return WebClient.builder()
            .baseUrl(myUrl)
            .clientConnector(new ReactorClientHttpConnector(httpClient))
            .build();
}
stakeika
  • 55
  • 1
  • 8