@
Slf4jpublic class HttpTraceLogFilter extends OncePerRequestFilter implements Ordered {
private static final String NEED_TRACE_PATH_PREFIX_BUSINESS = "/business";
private static final String NEED_TRACE_PATH_PREFIX_NPI = "/npi";
private static final String NEED_TRACE_PATH_PREFIX_SYSTEM = "/system";
private static final String IGNORE_CONTENT_TYPE = "multipart/form-data";
private final MeterRegistry registry;
public HttpTraceLogFilter(MeterRegistry registry) {
this.registry = registry;
}
@
Override public int getOrder() {
return Ordered.LOWEST_PRECEDENCE - 10;
}
@
Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
if (!isRequestValid(request)) {
filterChain.doFilter(request, response);
return;
}
if (!(request instanceof ContentCachingRequestWrapper)) {
request = new ContentCachingRequestWrapper(request);
}
if (!(response instanceof ContentCachingResponseWrapper)) {
response = new ContentCachingResponseWrapper(response);
}
int status = HttpStatus.INTERNAL_SERVER_ERROR.value();
long startTime = System.currentTimeMillis();
try {
filterChain.doFilter(request, response);
status = response.getStatus();
} finally {
String path = request.getRequestURI();
boolean pathFlag = path.startsWith(NEED_TRACE_PATH_PREFIX_BUSINESS) || path.startsWith(NEED_TRACE_PATH_PREFIX_NPI) || path.startsWith(NEED_TRACE_PATH_PREFIX_SYSTEM);
if (pathFlag && !Objects.equals(IGNORE_CONTENT_TYPE, request.getContentType())) {
String requestBody = IOUtils.toString(request.getInputStream(), Charsets.UTF_8);
log.info(requestBody);
//1. 记录日志
HttpTraceLog traceLog = new HttpTraceLog();
traceLog.setPath(path);
log.info("path ===================>: {}", path);
traceLog.setMethod(request.getMethod());
log.info("method ===================>: {}", request.getMethod());
long timeConsuming = System.currentTimeMillis() - startTime;
traceLog.setTimeTaken(timeConsuming);
log.info("timeConsuming ===================>: {} ms", timeConsuming);
//traceLog.setTime(LocalDateTime.now().toString());
//traceLog.setParameterMap(JsonMapper.INSTANCE.toJson(request.getParameterMap()));
traceLog.setStatus(status);
log.info("status ===================>: {}", status);
//traceLog.setRequestBody(getRequestBody(request));
//traceLog.setResponseBody(getResponseBody(response));
//
log.info("Http trace log: {}", JsonMapper.INSTANCE.toJson(traceLog));
//
log.info("Http trace log: {}===============================================>", traceLog);
}
updateResponse(response);
}
}
private boolean isRequestValid(HttpServletRequest request) {
try {
new URI(request.getRequestURL().toString());
return true;
} catch (URISyntaxException ex) {
return false;
}
}
private String getRequestBody(HttpServletRequest request) {
String requestBody = "";
ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
if (wrapper != null) {
try {
requestBody = IOUtils.toString(wrapper.getContentAsByteArray(), wrapper.getCharacterEncoding());
} catch (IOException e) {
// NOOP
}
}
return requestBody;
}
private String getResponseBody(HttpServletResponse response) {
String responseBody = "";
ContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse(response, ContentCachingResponseWrapper.class);
if (wrapper != null) {
try {
responseBody = IOUtils.toString(wrapper.getContentAsByteArray(), wrapper.getCharacterEncoding());
} catch (IOException e) {
// NOOP
}
}
return responseBody;
}
private void updateResponse(HttpServletResponse response) throws IOException {
ContentCachingResponseWrapper responseWrapper = WebUtils.getNativeResponse(response, ContentCachingResponseWrapper.class);
Objects.requireNonNull(responseWrapper).copyBodyToResponse();
}
@
Data private static class HttpTraceLog {
private String path;
private String parameterMap;
private String method;
private Long timeTaken;
private String time;
private Integer status;
private String requestBody;
private String responseBody;
}
}