diff --git a/src/main/java/cn/axzo/pokonyan/aop/RequestLog.java b/src/main/java/cn/axzo/pokonyan/aop/RequestLog.java index ac71afe..193f8b5 100644 --- a/src/main/java/cn/axzo/pokonyan/aop/RequestLog.java +++ b/src/main/java/cn/axzo/pokonyan/aop/RequestLog.java @@ -2,7 +2,6 @@ package cn.axzo.pokonyan.aop; import cn.axzo.pokonyan.util.RequestUtil; import cn.hutool.json.JSONUtil; -import javax.servlet.http.HttpServletRequest; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; @@ -12,6 +11,8 @@ import org.springframework.stereotype.Component; import org.springframework.util.StopWatch; import org.springframework.web.bind.annotation.RestController; +import javax.servlet.http.HttpServletRequest; + /** * 打印出入参及耗时 * @@ -23,29 +24,26 @@ import org.springframework.web.bind.annotation.RestController; @Component public class RequestLog { - final String checkDeath = "checkDeath"; + final String checkDeath = "checkDeath"; - @Around("@within(restController)||@annotation(restController)") - @SneakyThrows - public Object request(ProceedingJoinPoint joinPoint, RestController restController) { + @Around("@within(restController)||@annotation(restController)") + @SneakyThrows + public Object request(ProceedingJoinPoint joinPoint, RestController restController) { - HttpServletRequest request = RequestUtil.getRequest(); + HttpServletRequest request = RequestUtil.getRequest(); - if (request == null) { - return joinPoint.proceed(); + if (request == null) { + return joinPoint.proceed(); + } + if (request.getRequestURL().toString().contains(checkDeath)) { + return joinPoint.proceed(); + } + StopWatch stopWatch = new StopWatch(); + stopWatch.start(); + Object proceed = joinPoint.proceed(); + stopWatch.stop(); + log.info("[response]返回记录:responseParam = {} latency = {}", JSONUtil.toJsonStr(proceed), + stopWatch.getTotalTimeMillis()); + return proceed; } - if (request.getRequestURL().toString().contains(checkDeath)) { - Object proceed = joinPoint.proceed(); - return proceed; - } - StopWatch stopWatch = new StopWatch(); - stopWatch.start(); - Object proceed = joinPoint.proceed(); - stopWatch.stop(); - log.info("[response]返回记录:responseParam = {} latency = {}", JSONUtil.toJsonStr(proceed), - stopWatch.getTotalTimeMillis()); - return proceed; - } - - } diff --git a/src/main/java/cn/axzo/pokonyan/filter/RequestLogHandlerInterceptor.java b/src/main/java/cn/axzo/pokonyan/filter/RequestLogHandlerInterceptor.java index 5ec5be6..08a8eab 100644 --- a/src/main/java/cn/axzo/pokonyan/filter/RequestLogHandlerInterceptor.java +++ b/src/main/java/cn/axzo/pokonyan/filter/RequestLogHandlerInterceptor.java @@ -4,6 +4,7 @@ import cn.axzo.pokonyan.util.ExceptionUtil; import cn.axzo.pokonyan.wrapper.BodyReaderHttpServletRequestWrapper; import cn.hutool.json.JSONUtil; import com.alibaba.fastjson.JSON; +import lombok.NonNull; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.IOUtils; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -17,7 +18,7 @@ import javax.servlet.DispatcherType; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; -import java.util.Map; +import java.nio.charset.Charset; /** * 简单的打印一些入参 @@ -30,25 +31,21 @@ import java.util.Map; @Component public class RequestLogHandlerInterceptor implements HandlerInterceptor, WebMvcConfigurer { - private static final String SPLIT_EXCLUDE_PARAM = "\\|"; private static final String CHECK_DEATH_URL = "/checkDeath"; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //filter check death - StringBuffer requestUrl = request.getRequestURL(); if (request.getRequestURI().contains(CHECK_DEATH_URL)) { return true; } if (handler instanceof HandlerMethod) { - ExceptionUtil.ignoreException(() -> processRequestLog(request), (e) -> log.warn(e.getMessage())); + ExceptionUtil.ignoreException(() -> processRequestLog(request), (e) -> log.warn(e.getMessage(), e)); return true; - } - - log.info("[requestUrl:{}]", requestUrl); + log.info("[requestUrl:{}]", request.getRequestURL()); return true; } @@ -64,24 +61,20 @@ public class RequestLogHandlerInterceptor implements HandlerInterceptor, WebMvcC String method = request.getMethod(); StringBuffer requestUrl = request.getRequestURL(); - if ("get".equalsIgnoreCase(method)) { - log.info("[requestUrl:{}][method:{}][param:{}]", requestUrl, request.getMethod(), - JSON.toJSONString(request.getParameterMap())); - + log.info("[requestUrl:{}][method:{}][param:{}]", requestUrl, method, JSON.toJSONString(request.getParameterMap())); return; } if ("post".equalsIgnoreCase(method)) { - try { - String body = IOUtils.toString(request.getInputStream()); - boolean isJson = JSONUtil.isJsonObj(body); + String body = IOUtils.toString(request.getInputStream(), Charset.defaultCharset()); + boolean isJson = JSONUtil.isTypeJSON(body); boolean shortLength = body.length() < 4000; if (shortLength) { - log.info("[requestUrl:{}][method:{}][param:{}][body:{}]", requestUrl, request.getMethod(), - JSON.toJSONString(request.getParameterMap()), - isJson ? JSON.toJSONString(JSONUtil.toBean(body, Map.class)) : body); + body = isJson ? JSONUtil.toJsonStr(JSON.parseObject(body)) : body; + log.info("[requestUrl:{}][method:{}][param:{}][body:{}]", requestUrl, method, + JSON.toJSONString(request.getParameterMap()), body); return; } else { log.warn("request body too long, ignore print"); @@ -89,12 +82,12 @@ public class RequestLogHandlerInterceptor implements HandlerInterceptor, WebMvcC log.info("requestUrl:{}", requestUrl); } catch (IOException ioException) { - log.warn("打印参数失败[requestUrl:{}]", requestUrl); + log.warn("打印参数失败[requestUrl:{}]", requestUrl, ioException); } return; } - //other method + //other method log.info("requestUrl:{}", requestUrl); }