-
Notifications
You must be signed in to change notification settings - Fork 1
/
RouteClientLoggingInterceptor.java
37 lines (30 loc) · 1.49 KB
/
RouteClientLoggingInterceptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.ody.route.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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;
@Slf4j
@RequiredArgsConstructor
public class RouteClientLoggingInterceptor implements ClientHttpRequestInterceptor {
private final ObjectMapper objectMapper;
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
log.info("[RouteClient Request] Method: {}, URI: {}", request.getMethod(), request.getURI());
ClientHttpResponse response = execution.execute(request, body);
String responseBody = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8);
String singleLineBody = convertToSingleLine(responseBody);
log.info("[RouteClient Response] Status: {}, Body: {}", response.getStatusCode(), singleLineBody);
return response;
}
private String convertToSingleLine(String jsonString) throws IOException {
Object json = objectMapper.readValue(jsonString, Object.class);
return objectMapper.writeValueAsString(json);
}
}