13

I'm using Spring with RestTemplate to execute HTTP GET requests.

How can I log any request and response data to a log file automatically on each request?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • That doesn't help because when I intercept the message, I'd have java objects of type `org.springframework.http.client.InterceptingClientHttpRequest` and ` org.springframework.http.client.SimpleClientHttpResponse`. What I need is a text/xml representation of the response and log this one instead. – membersound Jul 17 '15 at 10:57
  • 2
    did you try to send messages passing an Apache httpClient to the restTempalte and to enable log at httpclient level? – Giovanni Jul 17 '15 at 12:05
  • read my answer from following:[how to log request and response rest call by RestTemplate] (https://stackoverflow.com/questions/3892018/how-do-i-log-response-in-spring-resttemplate/56003085#56003085) – Solanki Vaibhav May 06 '19 at 10:16
  • Read my answer to this question from the following : [answer](https://stackoverflow.com/questions/3892018/how-do-i-log-response-in-spring-resttemplate/56003085#56003085) – Solanki Vaibhav May 06 '19 at 10:18

1 Answers1

11

You can achieve this by using ClientHttpRequestInterceptor in Spring. You have to override method intercept of interface ClientHttpRequestInterceptor.

Below is the code snippet :

@Component
public class LogRequestResponseFilter implements ClientHttpRequestInterceptor {

private static final Logger logger=LoggerFactory.getLogger(LogRequestResponseFilter.class);

   @Override
   public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
         throws IOException {

      traceRequest(request, body);
      ClientHttpResponse clientHttpResponse = execution.execute(request, body);
      traceResponse(clientHttpResponse);

      return clientHttpResponse;
   } 

 private void traceRequest(HttpRequest request, byte[] body) throws IOException {
      logger.debug("request URI : " + request.getURI());
      logger.debug("request method : " + request.getMethod());
      logger.debug("request body : " + getRequestBody(body));
   }

   private String getRequestBody(byte[] body) throws UnsupportedEncodingException {
      if (body != null && body.length > 0) {
         return (new String(body, "UTF-8"));
      } else {
         return null;
      }
   }


  private void traceResponse(ClientHttpResponse response) throws IOException {
      String body = getBodyString(response);
      logger.debug("response status code: " + response.getStatusCode());
      logger.debug("response status text: " + response.getStatusText());
      logger.debug("response body : " + body);
   }

   private String getBodyString(ClientHttpResponse response) {
      try {
         if (response != null && response.getBody() != null) {// &&
                                                              // isReadableResponse(response))
                                                              // {
            StringBuilder inputStringBuilder = new StringBuilder();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getBody(), StandardCharsets.UTF_8));
            String line = bufferedReader.readLine();
            while (line != null) {
               inputStringBuilder.append(line);
               inputStringBuilder.append('\n');
               line = bufferedReader.readLine();
            }
            return inputStringBuilder.toString();
         } else {
            return null;
         }
      } catch (IOException e) {
         logger.error(e.getMessage(), e);
         return null;
      }
   }
Captain Man
  • 6,997
  • 6
  • 48
  • 74
bvyas
  • 384
  • 3
  • 10
  • Is it sufficient to just add `@Component` to the custom `Interceptor`? Or do I have to register it explicit somewhere, eg on a `RestTemplate`? Because that's what I'm trying to prevent: having to register and wire it myself. – membersound Dec 29 '16 at 15:13
  • I believe @Component is sufficient. You can follow below link which is good source of example. http://blog.cacoveanu.com/2016/06/24/rest-template-logging.html – bvyas Dec 29 '16 at 15:25
  • 4
    This logs correctly but what if the underlying `InputStream` doesn't support marking? Once it's read it's gone. – Captain Man Apr 27 '17 at 15:51
  • I get an error using this where tracing the response prevents it from being marshalled into my POJO later. Not sure why, but it helped for debugging nonetheless. – IcedDante Sep 19 '17 at 14:58