don't log as much for request logging

This commit is contained in:
Lee 2024-04-12 20:07:25 +01:00
parent f1dae95a4c
commit 53c50b3a05

@ -22,14 +22,13 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
@ControllerAdvice @ControllerAdvice
@Slf4j(topic = "Req/Res Transaction") @Slf4j(topic = "Req Transaction")
public class TransactionLogger implements ResponseBodyAdvice<Object> { public class TransactionLogger implements ResponseBodyAdvice<Object> {
@Override @Override
public Object beforeBodyWrite(Object body, @NonNull MethodParameter returnType, @NonNull MediaType selectedContentType, public Object beforeBodyWrite(Object body, @NonNull MethodParameter returnType, @NonNull MediaType selectedContentType,
@NonNull Class<? extends HttpMessageConverter<?>> selectedConverterType, @NonNull ServerHttpRequest rawRequest, @NonNull Class<? extends HttpMessageConverter<?>> selectedConverterType, @NonNull ServerHttpRequest rawRequest,
@NonNull ServerHttpResponse rawResponse) { @NonNull ServerHttpResponse rawResponse) {
HttpServletRequest request = ((ServletServerHttpRequest) rawRequest).getServletRequest(); HttpServletRequest request = ((ServletServerHttpRequest) rawRequest).getServletRequest();
HttpServletResponse response = ((ServletServerHttpResponse) rawResponse).getServletResponse();
// Get the request ip ip // Get the request ip ip
String ip = IPUtils.getRealIp(request); String ip = IPUtils.getRealIp(request);
@ -40,33 +39,12 @@ public class TransactionLogger implements ResponseBodyAdvice<Object> {
params.put(entry.getKey(), Arrays.toString(entry.getValue())); params.put(entry.getKey(), Arrays.toString(entry.getValue()));
} }
// Getting headers // Logging the request
Map<String, String> headers = new HashMap<>(); log.info(String.format("[Req] %s | %s | '%s', params=%s",
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
headers.put(headerName, request.getHeader(headerName));
}
// Log the request
log.info(String.format("[Req] %s | %s | '%s', params=%s, headers=%s",
request.getMethod(), request.getMethod(),
ip, ip,
request.getRequestURI(), request.getRequestURI(),
params, params
headers
));
// Getting response headers
headers = new HashMap<>();
for (String headerName : response.getHeaderNames()) {
headers.put(headerName, response.getHeader(headerName));
}
// Log the response
log.info(String.format("[Res] %s, headers=%s",
response.getStatus(),
headers
)); ));
return body; return body;
} }