优化了LogAspect的代码
This commit is contained in:
@@ -12,8 +12,10 @@ import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StopWatch;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Aspect
|
||||
@Slf4j
|
||||
@@ -30,27 +32,53 @@ public class LogAspect {
|
||||
|
||||
@Around("logPointCut()")
|
||||
public Object around(ProceedingJoinPoint point) throws Throwable {
|
||||
// 获取HTTP请求对象
|
||||
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
|
||||
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
if (servletRequestAttributes == null) {
|
||||
log.warn("无法获取请求上下文信息");
|
||||
return point.proceed();
|
||||
}
|
||||
|
||||
HttpServletRequest request = servletRequestAttributes.getRequest();
|
||||
HttpServletResponse resp = servletRequestAttributes.getResponse();
|
||||
// 获取请求URL
|
||||
HttpServletResponse response = servletRequestAttributes.getResponse();
|
||||
|
||||
String url = request.getRequestURI().toString();
|
||||
String uuid = resp.getHeader(requestIdKey);
|
||||
String uuid = response != null ? response.getHeader(requestIdKey) : null;
|
||||
|
||||
// 获取方法参数
|
||||
Object[] args = point.getArgs();
|
||||
String reqParam = Arrays.toString(args); // 直接使用 Arrays.toString()
|
||||
String reqParam = filterAndConvertArgsToString(args);
|
||||
|
||||
// 输出请求日志
|
||||
log.info("request start, path: {}, uuid: {}, params: {}", url, uuid, reqParam);
|
||||
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
stopWatch.start();
|
||||
Object res = point.proceed();
|
||||
stopWatch.stop();
|
||||
long totalTimeMillis = stopWatch.getTotalTimeMillis();
|
||||
log.info("request end, path: {}, uuid: {}, cost: {}ms", url, uuid, totalTimeMillis);
|
||||
return res;
|
||||
|
||||
try {
|
||||
Object res = point.proceed();
|
||||
stopWatch.stop();
|
||||
long totalTimeMillis = stopWatch.getTotalTimeMillis();
|
||||
log.info("request end, path: {}, uuid: {}, cost: {}ms", url, uuid, totalTimeMillis);
|
||||
return res;
|
||||
} catch (Throwable throwable) {
|
||||
stopWatch.stop();
|
||||
long totalTimeMillis = stopWatch.getTotalTimeMillis();
|
||||
log.error("request error, path: {}, uuid: {}, cost: {}ms", url, uuid, totalTimeMillis);
|
||||
throw throwable;
|
||||
}
|
||||
}
|
||||
|
||||
private String filterAndConvertArgsToString(Object[] args) {
|
||||
if (args == null || args.length == 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return Arrays.stream(args)
|
||||
.filter(arg -> !(arg instanceof HttpServletRequest ||
|
||||
arg instanceof HttpServletResponse ||
|
||||
arg instanceof MultipartFile ||
|
||||
(arg != null && arg.getClass().isArray() && arg.getClass().getComponentType() != null &&
|
||||
arg.getClass().getComponentType().isAssignableFrom(MultipartFile.class))))
|
||||
.map(String::valueOf)
|
||||
.collect(Collectors.joining(", ", "[", "]"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user