Merge branch 'perf/20240314' of axzsource.com:universal/infrastructure/backend/pokonyan into pre

This commit is contained in:
luofu 2024-03-19 09:46:18 +08:00
commit e5bf7576e6
2 changed files with 32 additions and 41 deletions

View File

@ -2,7 +2,6 @@ package cn.axzo.pokonyan.aop;
import cn.axzo.pokonyan.util.RequestUtil; import cn.axzo.pokonyan.util.RequestUtil;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import javax.servlet.http.HttpServletRequest;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
@ -12,6 +11,8 @@ import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch; import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
/** /**
* 打印出入参及耗时 * 打印出入参及耗时
* *
@ -23,29 +24,26 @@ import org.springframework.web.bind.annotation.RestController;
@Component @Component
public class RequestLog { public class RequestLog {
final String checkDeath = "checkDeath"; final String checkDeath = "checkDeath";
@Around("@within(restController)||@annotation(restController)") @Around("@within(restController)||@annotation(restController)")
@SneakyThrows @SneakyThrows
public Object request(ProceedingJoinPoint joinPoint, RestController restController) { public Object request(ProceedingJoinPoint joinPoint, RestController restController) {
HttpServletRequest request = RequestUtil.getRequest(); HttpServletRequest request = RequestUtil.getRequest();
if (request == null) { if (request == null) {
return joinPoint.proceed(); 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;
}
} }

View File

@ -4,6 +4,7 @@ import cn.axzo.pokonyan.util.ExceptionUtil;
import cn.axzo.pokonyan.wrapper.BodyReaderHttpServletRequestWrapper; import cn.axzo.pokonyan.wrapper.BodyReaderHttpServletRequestWrapper;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@ -17,7 +18,7 @@ import javax.servlet.DispatcherType;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.nio.charset.Charset;
/** /**
* 简单的打印一些入参 * 简单的打印一些入参
@ -30,25 +31,21 @@ import java.util.Map;
@Component @Component
public class RequestLogHandlerInterceptor implements HandlerInterceptor, WebMvcConfigurer { public class RequestLogHandlerInterceptor implements HandlerInterceptor, WebMvcConfigurer {
private static final String SPLIT_EXCLUDE_PARAM = "\\|";
private static final String CHECK_DEATH_URL = "/checkDeath"; private static final String CHECK_DEATH_URL = "/checkDeath";
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//filter check death //filter check death
StringBuffer requestUrl = request.getRequestURL();
if (request.getRequestURI().contains(CHECK_DEATH_URL)) { if (request.getRequestURI().contains(CHECK_DEATH_URL)) {
return true; return true;
} }
if (handler instanceof HandlerMethod) { 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; return true;
} }
log.info("[requestUrl:{}]", request.getRequestURL());
log.info("[requestUrl:{}]", requestUrl);
return true; return true;
} }
@ -64,24 +61,20 @@ public class RequestLogHandlerInterceptor implements HandlerInterceptor, WebMvcC
String method = request.getMethod(); String method = request.getMethod();
StringBuffer requestUrl = request.getRequestURL(); StringBuffer requestUrl = request.getRequestURL();
if ("get".equalsIgnoreCase(method)) { if ("get".equalsIgnoreCase(method)) {
log.info("[requestUrl:{}][method:{}][param:{}]", requestUrl, request.getMethod(), log.info("[requestUrl:{}][method:{}][param:{}]", requestUrl, method, JSON.toJSONString(request.getParameterMap()));
JSON.toJSONString(request.getParameterMap()));
return; return;
} }
if ("post".equalsIgnoreCase(method)) { if ("post".equalsIgnoreCase(method)) {
try { try {
String body = IOUtils.toString(request.getInputStream()); String body = IOUtils.toString(request.getInputStream(), Charset.defaultCharset());
boolean isJson = JSONUtil.isJsonObj(body); boolean isJson = JSONUtil.isTypeJSON(body);
boolean shortLength = body.length() < 4000; boolean shortLength = body.length() < 4000;
if (shortLength) { if (shortLength) {
log.info("[requestUrl:{}][method:{}][param:{}][body:{}]", requestUrl, request.getMethod(), body = isJson ? JSONUtil.toJsonStr(JSON.parseObject(body)) : body;
JSON.toJSONString(request.getParameterMap()), log.info("[requestUrl:{}][method:{}][param:{}][body:{}]", requestUrl, method,
isJson ? JSON.toJSONString(JSONUtil.toBean(body, Map.class)) : body); JSON.toJSONString(request.getParameterMap()), body);
return; return;
} else { } else {
log.warn("request body too long, ignore print"); log.warn("request body too long, ignore print");
@ -89,12 +82,12 @@ public class RequestLogHandlerInterceptor implements HandlerInterceptor, WebMvcC
log.info("requestUrl:{}", requestUrl); log.info("requestUrl:{}", requestUrl);
} catch (IOException ioException) { } catch (IOException ioException) {
log.warn("打印参数失败[requestUrl:{}]", requestUrl); log.warn("打印参数失败[requestUrl:{}]", requestUrl, ioException);
} }
return; return;
} }
//other method //other method
log.info("requestUrl:{}", requestUrl); log.info("requestUrl:{}", requestUrl);
} }