This commit is contained in:
luke 2025-06-22 14:50:07 +08:00
parent fe00238532
commit a222e64d24

View File

@ -86,23 +86,35 @@ public class LogFilter extends OncePerRequestFilter {
String tranCode = Optional.ofNullable(request.getHeader("TRANCODE")).orElse("UNKNOWN"); String tranCode = Optional.ofNullable(request.getHeader("TRANCODE")).orElse("UNKNOWN");
String remoteIp = getRemoteIP(request); String remoteIp = getRemoteIP(request);
int remotePort = request.getRemotePort(); int remotePort = request.getRemotePort();
String traceId = MDC.get(MDCKeyEnum.TRACE_ID.name);
String reqBody = extractRequestBody(request); String reqBody = extractRequestBody(request);
String respBody = extractResponseBody(response); String respBody = extractResponseBody(response);
Map<String, Object> logMap = new HashMap<>(); LOGGER.info("交易跟踪号[{}]\n请求路径[{}]\n交易码[{}]\n远程地址[{}]\n远程端口[{}]\n请求内容:[{}]\n响应内容:[{}]\n响应时间:[{}]毫秒",
logMap.put("traceId", MDC.get(MDCKeyEnum.TRACE_ID.name)); traceId,
logMap.put("url", url); url,
logMap.put("tranCode", tranCode); tranCode,
logMap.put("ip", remoteIp); remoteIp,
logMap.put("port", remotePort); remotePort,
logMap.put("request", reqBody); reqBody,
logMap.put("response", respBody); respBody,
logMap.put("timeMs", timeCostMs); timeCostMs
);
LOGGER.info("log_trace_record: {}", JSONUtil.toJsonStr(logMap)); Map<String, Object> jsonLog = new HashMap<>();
jsonLog.put("traceId", traceId);
jsonLog.put("url", url);
jsonLog.put("tranCode", tranCode);
jsonLog.put("ip", remoteIp);
jsonLog.put("port", remotePort);
jsonLog.put("request", reqBody);
jsonLog.put("response", respBody);
jsonLog.put("timeMs", timeCostMs);
LOGGER.info("log_trace_record: {}", JSONUtil.toJsonStr(jsonLog));
} }
private String extractRequestBody(ContentCachingRequestWrapper request) { private String extractRequestBody(ContentCachingRequestWrapper request) {
String contentType = request.getContentType(); String contentType = request.getContentType();
byte[] content = request.getContentAsByteArray(); byte[] content = request.getContentAsByteArray();