how to log Spring 5 WebClient call

前端 未结 8 844
清酒与你
清酒与你 2020-11-29 03:13

I\'m trying to log a request using Spring 5 WebClient. Do you have any idea how could I achieve that?

(I\'m using Spring 5 and Spring boot 2)

The code looks

8条回答
  •  天命终不由人
    2020-11-29 03:31

    An update of Feb 2020 for Spring Boot 2.2.4 and Spring 5.2.3:

    I did not manage to get spring.http.log-request-details=true doing its job, and current Spring WebFlux reference suggests that some coding needs be done to have headers logged, though the code example uses deprecated exchangeStrategies() method.

    There is still a replacement for the deprecated method, so a compact piece of code for getting headers logged at WebClient level may look like this:

    WebClient webClient = WebClient.builder()
        .codecs(configurer -> configurer.defaultCodecs().enableLoggingRequestDetails(true))
        .build();
    

    with further

    logging.level.org.springframework.web.reactive.function.client.ExchangeFunctions=TRACE
    

    It should be noted though that not all of the headers are available (do exist) at WebFlux ExchangeFunctions level, so some more logging at Netty HttpClient level may be essential too, as per @Matthew's suggestion:

    WebClient webClient = WebClient.builder()
        .clientConnector(new ReactorClientHttpConnector(
            HttpClient.create()
                .wiretap(true)))
        .build()
    

    with further

    logging.level.reactor.netty.http.client.HttpClient: DEBUG
    

    This will get bodies logged too.

提交回复
热议问题