org.springframework.web.client.logger.RestTemplateLoggingInterceptor Maven / Gradle / Ivy
The newest version!
package org.springframework.web.client.logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
public class RestTemplateLoggingInterceptor implements ClientHttpRequestInterceptor {
private static Logger log = LoggerFactory.getLogger(RestTemplateLoggingInterceptor.class);
private final HttpLogLevel logLevel;
public RestTemplateLoggingInterceptor(HttpLogLevel logLevel) {
this.logLevel = logLevel;
}
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
if (HttpLogLevel.NONE == logLevel) {
return execution.execute(request, body);
}
traceRequest(request, body, logLevel);
long startNs = System.nanoTime();
ClientHttpResponse response;
if (HttpLogLevel.BODY.lte(logLevel)) {
response = new ClientHttpResponseLogWrapper(execution.execute(request, body));
} else {
response = execution.execute(request, body);
}
traceResponse(response, logLevel, startNs);
return response;
}
private static void traceRequest(HttpRequest request, byte[] body, HttpLogLevel logLevel) {
boolean hasBody = body != null && body.length > 0;
/*POST /greeting (3-byte body)*/
if (hasBody) {
log.info("--> {} {} ({}-byte body)", request.getMethod(), request.getURI(), body.length);
} else {
log.info("--> {} {}", request.getMethod(), request.getURI());
}
if (HttpLogLevel.HEADERS.lte(logLevel)) {
HttpHeaders headers = request.getHeaders();
headers.forEach(
(name, values) -> log.info("{}: {}", name, StringUtils.collectionToCommaDelimitedString(values)));
}
if (hasBody && HttpLogLevel.BODY.lte(logLevel)) {
log.info("Request body: {}", new String(body, StandardCharsets.UTF_8));
}
log.info("--> END {}", request.getMethod());
}
private static void traceResponse(ClientHttpResponse response, HttpLogLevel logLevel, long startNs)
throws IOException {
long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
log.info("<--{} {}({}ms)", response.getRawStatusCode(), response.getStatusText(), tookMs);
if (HttpLogLevel.HEADERS.lte(logLevel)) {
HttpHeaders headers = response.getHeaders();
headers.forEach(
(name, values) -> log.info("{}: {}", name, StringUtils.collectionToCommaDelimitedString(values)));
}
if (HttpLogLevel.BODY.lte(logLevel)) {
log.info("Response body: {}", StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8));
}
log.info("<-- END HTTP");
}
}