|
@@ -0,0 +1,91 @@
|
|
|
|
|
+package com.etotem.cfc.config;
|
|
|
|
|
+
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
+import org.springframework.core.annotation.Order;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
+
|
|
|
|
|
+import javax.servlet.*;
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+
|
|
|
|
|
+@Component
|
|
|
|
|
+@Order(Integer.MIN_VALUE + 1)
|
|
|
|
|
+public class ErrorLoggingFilter implements Filter {
|
|
|
|
|
+
|
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(ErrorLoggingFilter.class);
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
|
|
|
|
+ throws IOException, ServletException {
|
|
|
|
|
+
|
|
|
|
|
+ HttpServletRequest req = (HttpServletRequest) request;
|
|
|
|
|
+ HttpServletResponse resp = (HttpServletResponse) response;
|
|
|
|
|
+
|
|
|
|
|
+ if (!shouldLog(req)) {
|
|
|
|
|
+ chain.doFilter(request, response);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ StatusExposingHttpServletResponse wrapped = new StatusExposingHttpServletResponse(resp);
|
|
|
|
|
+ Throwable caught = null;
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ chain.doFilter(request, wrapped);
|
|
|
|
|
+ } catch (Throwable t) {
|
|
|
|
|
+ caught = t;
|
|
|
|
|
+ log.error("HTTP {} {} {} - 异常: {} - 返回 500",
|
|
|
|
|
+ req.getMethod(), req.getRequestURI(),
|
|
|
|
|
+ req.getQueryString() != null ? "?" + req.getQueryString() : "",
|
|
|
|
|
+ t.getClass().getName() + ": " + t.getMessage());
|
|
|
|
|
+ // 继续传播,让 Spring 处理异常
|
|
|
|
|
+ throw t;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ int status = wrapped.getStatus();
|
|
|
|
|
+
|
|
|
|
|
+ if (caught == null && status >= 500) {
|
|
|
|
|
+ log.error("HTTP {} {} {} - 返回 {}", req.getMethod(), req.getRequestURI(),
|
|
|
|
|
+ req.getQueryString() != null ? "?" + req.getQueryString() : "", status);
|
|
|
|
|
+ } else if (caught == null && status >= 400) {
|
|
|
|
|
+ log.warn("HTTP {} {} {} - 返回 {}", req.getMethod(), req.getRequestURI(),
|
|
|
|
|
+ req.getQueryString() != null ? "?" + req.getQueryString() : "", status);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private boolean shouldLog(HttpServletRequest req) {
|
|
|
|
|
+ String uri = req.getRequestURI();
|
|
|
|
|
+ return uri.startsWith("/api/") && !"OPTIONS".equals(req.getMethod());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static class StatusExposingHttpServletResponse extends javax.servlet.http.HttpServletResponseWrapper {
|
|
|
|
|
+ private int status = 200;
|
|
|
|
|
+
|
|
|
|
|
+ public StatusExposingHttpServletResponse(HttpServletResponse response) {
|
|
|
|
|
+ super(response);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void setStatus(int code) {
|
|
|
|
|
+ this.status = code;
|
|
|
|
|
+ super.setStatus(code);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void sendError(int code) throws IOException {
|
|
|
|
|
+ this.status = code;
|
|
|
|
|
+ super.sendError(code);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void sendError(int code, String message) throws IOException {
|
|
|
|
|
+ this.status = code;
|
|
|
|
|
+ super.sendError(code, message);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public int getStatus() {
|
|
|
|
|
+ return status;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|