how to log Spring 5 WebClient call

前端 未结 8 817
清酒与你
清酒与你 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:32

    @StasKolodyuk's answer elaborates on the solution from baeldung for logging the response body of a reactive WebClient. Note that

    tc.bootstrap(...)
    

    is deprecated in

        HttpClient httpClient = HttpClient
          .create()
          .tcpConfiguration(
            tc -> tc.bootstrap(
              b -> BootstrapHandlers.updateLogSupport(b, new CustomLogger(HttpClient.class))))
          .build()
    

    Another non-deprecated way to add your custom LoggingHandler is (Kotlin)

    val httpClient: HttpClient = HttpClient.create().mapConnect { conn, b ->
        BootstrapHandlers.updateLogSupport(b, CustomLogger(HttpClient::class.java))
        conn
    }
    

提交回复
热议问题