SpringRestTemplate-日志降级到debug

This commit is contained in:
khalil
2025-05-13 12:21:33 +08:00
parent 45c9325f20
commit f9708a9d3b

View File

@@ -35,22 +35,26 @@ public class RestTemplateLoggerInterceptor implements ClientHttpRequestIntercept
}
private void logRequest(HttpRequest request, byte[] body) {
String uri = request.getURI().toString();
String method = request.getMethod().name();
String headers = request.getHeaders().toString();
String bodyStr = new String(body, StandardCharsets.UTF_8);
if (log.isDebugEnabled()){
String uri = request.getURI().toString();
String method = request.getMethod().name();
String headers = request.getHeaders().toString();
String bodyStr = new String(body, StandardCharsets.UTF_8);
log.info("[REQ] URI: {} | Method: {} | Headers: {} | Body: {}",
uri, method, headers, bodyStr);
log.debug("[REQ] URI: {} | Method: {} | Headers: {} | Body: {}",
uri, method, headers, bodyStr);
}
}
private void logResponse(ClientHttpResponse response, long duration, String uri) throws IOException {
HttpStatus status = response.getStatusCode();
String headers = response.getHeaders().toString();
String body = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8);
if (log.isDebugEnabled()){
HttpStatus status = response.getStatusCode();
String headers = response.getHeaders().toString();
String body = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8);
log.info("[RES] URI: {} | Status: {} {} | Headers: {} | Body: {} | Duration: {}ms",
uri, status.value(), status.getReasonPhrase(), headers, body, duration);
log.error("[RES] URI: {} | Status: {} {} | Headers: {} | Body: {} | Duration: {}ms",
uri, status.value(), status.getReasonPhrase(), headers, body, duration);
}
}
}