Log requests made by the SpringBoot application to external services

1.1k Views Asked by At

When I enable http trace in SpringBoot application (https://stackoverflow.com/a/59115579/6700081) then using the /httptrace endpoint I am able to view all the requests (only headers. not request body) that hit the application.

Is it possible to also view all the requests sent out from the application to other applications like Rest services, webservices, etc in /httptrace endpoint?

I want to know what are all the external services that my application is connecting to, when I send a particular request to the application

1

There are 1 best solutions below

0
On

if you are using RestTemplate for your Rest call, then you can add an ClientHttpRequestInterceptor as suggested by gtiwari333.

    public class LoggingInterceptor implements ClientHttpRequestInterceptor {
     
        static Logger log = LoggerFactory.getLogger(LoggingInterceptor.class);
     
        @Override
        public ClientHttpResponse intercept(HttpRequest req, byte[] reqBody, ClientHttpRequestExecution ex)
          throws IOException {
            log.debug("Request body: {}", new String(reqBody, StandardCharsets.UTF_8));
            ClientHttpResponse response = ex.execute(req, reqBody);
            InputStreamReader isr = new InputStreamReader(response.getBody(), StandardCharsets.UTF_8);
            String body = new BufferedReader(isr)
              .lines()
              .collect(Collectors.joining("\n"));
            log.debug("Response body: {}", body);
            return response;
        }
    }