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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| package com.ruoyi.framework.config.web;
import com.baomidou.mybatisplus.core.toolkit.SystemClock; import com.ruoyi.common.utils.uuid.IdUtils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.slf4j.MDC; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Map;
import static com.ruoyi.common.constant.Constants.TRACE_ID;
@Slf4j public class LogInterceptor implements HandlerInterceptor {
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception { request.setAttribute("start_time", SystemClock.now()); String ipAddress = RequestUtil.getIPAddress(request); String requestURI = request.getRequestURI(); Map<String, String> parameterMap = RequestUtil.parseRequestParam(request); Map<String, String> headersMap = RequestUtil.parseRequestHeaderParam(request); MDC.put(TRACE_ID, IdUtils.fastSimpleUUID()); log.info(">>请求日志:请求的api={},请求参数={},请求header参数={},请求的ip={}", requestURI, parameterMap, headersMap, ipAddress); return true; }
@Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3) { String ipAddress = RequestUtil.getIPAddress(request); String requestURI = request.getRequestURI(); String responseParameter;
if (requestURI != null && requestURI.contains("export") && ResponseUtil.getResponseDataSize(response) > 200) { responseParameter = "该请求为下载文件类型请求不显示具体值"; } else if (StringUtils.isNotEmpty(response.getHeader("Content-Disposition")) && ResponseUtil.getResponseDataSize(response) > 200) { responseParameter = "响应为下载类型不显示具体值,Content-Disposition值为" + response.getHeader("Content-Disposition"); } else if (requestURI.startsWith("/view")) { responseParameter = "响应为静态页面类型不显示具体值";
} else { responseParameter = ResponseUtil.getResponseParameter(response); } long startTime = (long) request.getAttribute("start_time"); long endTime = SystemClock.now(); long total = endTime - startTime; log.info(">>响应日志:请求的api={},返回参数={},请求的ip={},接口耗时毫秒值={}", requestURI, responseParameter, ipAddress, total); MDC.remove(TRACE_ID); }
@Override public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) { }
}
|