fix(LogAspect): 修复日志切面方法执行耗时统计错误
This commit is contained in:
@@ -19,10 +19,8 @@ import jakarta.servlet.http.HttpServletResponse;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.aspectj.lang.JoinPoint;
|
import org.aspectj.lang.JoinPoint;
|
||||||
import org.aspectj.lang.annotation.AfterReturning;
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
import org.aspectj.lang.annotation.AfterThrowing;
|
import org.aspectj.lang.annotation.*;
|
||||||
import org.aspectj.lang.annotation.Aspect;
|
|
||||||
import org.aspectj.lang.annotation.Pointcut;
|
|
||||||
import org.springframework.cache.CacheManager;
|
import org.springframework.cache.CacheManager;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.context.request.RequestContextHolder;
|
import org.springframework.web.context.request.RequestContextHolder;
|
||||||
@@ -61,23 +59,25 @@ public class LogAspect {
|
|||||||
*
|
*
|
||||||
* @param joinPoint 切点
|
* @param joinPoint 切点
|
||||||
*/
|
*/
|
||||||
@AfterReturning(pointcut = "logPointcut() && @annotation(logAnnotation)", returning = "jsonResult")
|
@Around("logPointcut() && @annotation(logAnnotation)")
|
||||||
public void doAfterReturning(JoinPoint joinPoint, com.youlai.boot.common.annotation.Log logAnnotation, Object jsonResult) {
|
public Object doAround(ProceedingJoinPoint joinPoint, com.youlai.boot.common.annotation.Log logAnnotation) throws Throwable {
|
||||||
this.saveLog(joinPoint, null, jsonResult, logAnnotation);
|
TimeInterval timer = DateUtil.timer();
|
||||||
|
Object result = null;
|
||||||
|
Exception exception = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
result = joinPoint.proceed();
|
||||||
|
} catch (Exception e) {
|
||||||
|
exception = e;
|
||||||
|
throw e;
|
||||||
|
} finally {
|
||||||
|
long executionTime = timer.interval(); // 执行时长
|
||||||
|
this.saveLog(joinPoint, exception, result, logAnnotation, executionTime);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 拦截异常操作
|
|
||||||
*
|
|
||||||
* @param joinPoint 切点
|
|
||||||
* @param e 异常
|
|
||||||
*/
|
|
||||||
@AfterThrowing(value = "logPointcut()", throwing = "e")
|
|
||||||
public void doAfterThrowing(JoinPoint joinPoint, Exception e) {
|
|
||||||
this.saveLog(joinPoint, e, null, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 保存日志
|
* 保存日志
|
||||||
*
|
*
|
||||||
@@ -86,15 +86,11 @@ public class LogAspect {
|
|||||||
* @param jsonResult 响应结果
|
* @param jsonResult 响应结果
|
||||||
* @param logAnnotation 日志注解
|
* @param logAnnotation 日志注解
|
||||||
*/
|
*/
|
||||||
private void saveLog(final JoinPoint joinPoint, final Exception e, Object jsonResult, com.youlai.boot.common.annotation.Log logAnnotation) {
|
private void saveLog(final JoinPoint joinPoint, final Exception e, Object jsonResult, com.youlai.boot.common.annotation.Log logAnnotation, long executionTime) {
|
||||||
String requestURI = request.getRequestURI();
|
String requestURI = request.getRequestURI();
|
||||||
|
|
||||||
TimeInterval timer = DateUtil.timer();
|
|
||||||
// 执行方法
|
|
||||||
long executionTime = timer.interval();
|
|
||||||
|
|
||||||
// 创建日志记录
|
// 创建日志记录
|
||||||
Log log = new Log();
|
Log log = new Log();
|
||||||
|
log.setExecutionTime(executionTime);
|
||||||
if (logAnnotation == null && e != null) {
|
if (logAnnotation == null && e != null) {
|
||||||
log.setModule(LogModuleEnum.EXCEPTION);
|
log.setModule(LogModuleEnum.EXCEPTION);
|
||||||
log.setContent("系统发生异常");
|
log.setContent("系统发生异常");
|
||||||
@@ -129,7 +125,7 @@ public class LogAspect {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.setExecutionTime(executionTime);
|
|
||||||
// 获取浏览器和终端系统信息
|
// 获取浏览器和终端系统信息
|
||||||
String userAgentString = request.getHeader("User-Agent");
|
String userAgentString = request.getHeader("User-Agent");
|
||||||
UserAgent userAgent = resolveUserAgent(userAgentString);
|
UserAgent userAgent = resolveUserAgent(userAgentString);
|
||||||
|
|||||||
Reference in New Issue
Block a user