diff --git a/hzims-biz-common/src/main/java/com/hnac/hzims/common/logs/aop/SysLogAspect.java b/hzims-biz-common/src/main/java/com/hnac/hzims/common/logs/aop/SysLogAspect.java index bcecd2b..75e217c 100644 --- a/hzims-biz-common/src/main/java/com/hnac/hzims/common/logs/aop/SysLogAspect.java +++ b/hzims-biz-common/src/main/java/com/hnac/hzims/common/logs/aop/SysLogAspect.java @@ -18,6 +18,7 @@ import org.springblade.core.tool.constant.BladeConstant; import org.springblade.core.tool.utils.Func; import org.springblade.core.tool.utils.WebUtil; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.NamedThreadLocal; import org.springframework.stereotype.Component; import org.springframework.util.StopWatch; import org.springframework.web.context.request.RequestAttributes; @@ -52,37 +53,10 @@ public class SysLogAspect { @Autowired private SysLogQueue sysLogQueue; - /** - * 请求地址 - */ - private String requestPath = null; - - /** - * 操作人 - */ - private String userName = null; - - /** - * 请求 - */ - private HttpServletRequest request = null; - - private Long userId = -1L; + //private StopWatch stopWatch = new StopWatch(); - - private StopWatch stopWatch = new StopWatch(); - - - /** - * 开始时间 - */ - private Long startTime = 0L; - - /** - * 结束时间 - */ - private Long endTime = 0L; + private static final ThreadLocal TIME_THREADLOCAL = new NamedThreadLocal<>("Cost Time"); /** @@ -101,7 +75,8 @@ public class SysLogAspect { // if (!stopWatch.isRunning()) { // stopWatch.start(); // } - startTime = System.currentTimeMillis(); + //startTime = System.currentTimeMillis(); + TIME_THREADLOCAL.set(System.currentTimeMillis()); log.info("前置通知"); } @@ -112,8 +87,14 @@ public class SysLogAspect { @AfterReturning(value = "logPointCut()", returning = "jsonResult") public void after(JoinPoint joinPoint, Object jsonResult) { log.info("=========返回通知=============="); - request = getHttpServletRequest(); - handleLog(joinPoint, jsonResult, null); + try { + handleLog(joinPoint, jsonResult, null); + } catch (Exception e) { + log.error("异常信息:{}", e.getMessage()); + e.printStackTrace(); + } finally { + TIME_THREADLOCAL.remove(); + } } @@ -153,7 +134,14 @@ public class SysLogAspect { @AfterThrowing(pointcut = "logPointCut()", throwing = "e") public void throwing(JoinPoint joinPoint, Exception e) { log.info("=========异常通知=============="); - handleLog(joinPoint, null, e); + try { + handleLog(joinPoint, null, e); + } catch (Exception exception) { + log.error("异常信息:{}", exception.getMessage()); + e.printStackTrace(); + } finally { + TIME_THREADLOCAL.remove(); + } } @@ -218,8 +206,8 @@ public class SysLogAspect { // 获取请求的类名 String className = joinPoint.getTarget().getClass().getName(); methodName = className + "." + methodName; - request = getHttpServletRequest(); - requestPath = request.getServletPath(); + HttpServletRequest request = getHttpServletRequest(); + String requestPath = request.getServletPath(); OperationAnnotation annotation = method.getAnnotation(OperationAnnotation.class); title = annotation.title(); action = annotation.action(); @@ -227,8 +215,8 @@ public class SysLogAspect { operatorType = annotation.operatorType().getValue(); moduleName = annotation.moduleName(); // 获取当前用户信息 - userName = AuthUtil.getUserAccount(request); - userId = AuthUtil.getUserId(request); + String userName = AuthUtil.getUserAccount(request); + Long userId = AuthUtil.getUserId(request); SysLogTo sysLog = new SysLogTo(); if (StringUtils.isBlank(userName) && userId == -1) { userName = "当前用户未登录"; @@ -241,8 +229,8 @@ public class SysLogAspect { // if (stopWatch.isRunning()) { // stopWatch.stop(); // } - endTime = System.currentTimeMillis(); - sysLog.setCostTime((endTime - startTime) + "ms"); + long endTime = System.currentTimeMillis(); + sysLog.setCostTime((endTime - TIME_THREADLOCAL.get()) + "ms"); sysLog.setPath(requestPath); sysLog.setTitle(title); sysLog.setAction(action);