Created
April 9, 2020 15:01
-
-
Save yusufcakal/30db64c72e5db20f699bd031bbd9d13c to your computer and use it in GitHub Desktop.
For using display logs that request and response to any http request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component | |
public class RequestResponseLoggingInterceptor implements ClientHttpRequestInterceptor { | |
private final Logger log = LoggerFactory.getLogger(this.getClass()); | |
@Override | |
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { | |
logRequest(request, body); | |
ClientHttpResponse response = execution.execute(request, body); | |
logResponse(response); | |
return response; | |
} | |
private void logRequest(HttpRequest request, byte[] body) throws IOException { | |
log.info("===========================request begin================================================"); | |
log.info("URI : {}", request.getURI()); | |
log.info("Method : {}", request.getMethod()); | |
log.info("Headers : {}", request.getHeaders()); | |
log.info("Request body: {}", new String(body, "UTF-8")); | |
log.info("==========================request end================================================"); | |
} | |
private void logResponse(ClientHttpResponse response) throws IOException { | |
log.info("============================response begin=========================================="); | |
log.info("Status code : {}", response.getStatusCode()); | |
log.info("Status text : {}", response.getStatusText()); | |
log.info("Headers : {}", response.getHeaders()); | |
log.info("Response body: {}", StreamUtils.copyToString(response.getBody(), Charset.defaultCharset())); | |
log.info("=======================response end================================================="); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment