spring-cloud-gateway icon indicating copy to clipboard operation
spring-cloud-gateway copied to clipboard

how to log response body in exception handler

Open chengtangzheng2021 opened this issue 1 year ago • 3 comments

need to log the response body in exception handler, but the response doesn't have a getBody method, search a lots, but have not found a solution for this need

Just need to log the response body of the case of exception, not for every request processing

chengtangzheng2021 avatar Jan 25 '24 17:01 chengtangzheng2021

Chat GPT gave this code, but there is no getBody(). Chat GPT keeping giving code using getBody()

exchange.getResponse().getBody() .map(dataBuffer -> { byte[] bytes = new byte[dataBuffer.readableByteCount()]; dataBuffer.read(bytes); // Assuming UTF-8 encoding, you can change it based on your application's encoding return new String(bytes, java.nio.charset.StandardCharsets.UTF_8); }) .doOnNext(responseBody -> { // Do something with the response body System.out.println("Response Body: " + responseBody); }) .subscribe();

chengtangzheng2021 avatar Jan 25 '24 17:01 chengtangzheng2021

the ServerHttpResponseDecorator is loging response body for every request processing, our need is to log the response body just in exception case

chengtangzheng2021 avatar Jan 25 '24 18:01 chengtangzheng2021

our exception logging standard has some required logging fields besides the logging response body in a same json logging message logged in the exception handler

chengtangzheng2021 avatar Jan 25 '24 18:01 chengtangzheng2021

Have you tried https://docs.spring.io/spring-framework/reference/web/webflux/reactive-spring.html#webflux-exception-handler

spencergibb avatar Mar 11 '24 17:03 spencergibb

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-cloud-issues avatar Mar 18 '24 17:03 spring-cloud-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

spring-cloud-issues avatar Mar 25 '24 17:03 spring-cloud-issues