Browse Source

修改 aop日志输出

zhongwei
段飞宇 2 years ago
parent
commit
5ab8e62bcc
  1. 66
      hzims-biz-common/src/main/java/com/hnac/hzims/common/logs/aop/SysLogAspect.java

66
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.Func;
import org.springblade.core.tool.utils.WebUtil; import org.springblade.core.tool.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.NamedThreadLocal;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch; import org.springframework.util.StopWatch;
import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestAttributes;
@ -52,37 +53,10 @@ public class SysLogAspect {
@Autowired @Autowired
private SysLogQueue sysLogQueue; 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 static final ThreadLocal<Long> TIME_THREADLOCAL = new NamedThreadLocal<>("Cost Time");
private StopWatch stopWatch = new StopWatch();
/**
* 开始时间
*/
private Long startTime = 0L;
/**
* 结束时间
*/
private Long endTime = 0L;
/** /**
@ -101,7 +75,8 @@ public class SysLogAspect {
// if (!stopWatch.isRunning()) { // if (!stopWatch.isRunning()) {
// stopWatch.start(); // stopWatch.start();
// } // }
startTime = System.currentTimeMillis(); //startTime = System.currentTimeMillis();
TIME_THREADLOCAL.set(System.currentTimeMillis());
log.info("前置通知"); log.info("前置通知");
} }
@ -112,8 +87,14 @@ public class SysLogAspect {
@AfterReturning(value = "logPointCut()", returning = "jsonResult") @AfterReturning(value = "logPointCut()", returning = "jsonResult")
public void after(JoinPoint joinPoint, Object jsonResult) { public void after(JoinPoint joinPoint, Object jsonResult) {
log.info("=========返回通知=============="); log.info("=========返回通知==============");
request = getHttpServletRequest(); try {
handleLog(joinPoint, jsonResult, null); 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") @AfterThrowing(pointcut = "logPointCut()", throwing = "e")
public void throwing(JoinPoint joinPoint, Exception e) { public void throwing(JoinPoint joinPoint, Exception e) {
log.info("=========异常通知=============="); 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(); String className = joinPoint.getTarget().getClass().getName();
methodName = className + "." + methodName; methodName = className + "." + methodName;
request = getHttpServletRequest(); HttpServletRequest request = getHttpServletRequest();
requestPath = request.getServletPath(); String requestPath = request.getServletPath();
OperationAnnotation annotation = method.getAnnotation(OperationAnnotation.class); OperationAnnotation annotation = method.getAnnotation(OperationAnnotation.class);
title = annotation.title(); title = annotation.title();
action = annotation.action(); action = annotation.action();
@ -227,8 +215,8 @@ public class SysLogAspect {
operatorType = annotation.operatorType().getValue(); operatorType = annotation.operatorType().getValue();
moduleName = annotation.moduleName(); moduleName = annotation.moduleName();
// 获取当前用户信息 // 获取当前用户信息
userName = AuthUtil.getUserAccount(request); String userName = AuthUtil.getUserAccount(request);
userId = AuthUtil.getUserId(request); Long userId = AuthUtil.getUserId(request);
SysLogTo sysLog = new SysLogTo(); SysLogTo sysLog = new SysLogTo();
if (StringUtils.isBlank(userName) && userId == -1) { if (StringUtils.isBlank(userName) && userId == -1) {
userName = "当前用户未登录"; userName = "当前用户未登录";
@ -241,8 +229,8 @@ public class SysLogAspect {
// if (stopWatch.isRunning()) { // if (stopWatch.isRunning()) {
// stopWatch.stop(); // stopWatch.stop();
// } // }
endTime = System.currentTimeMillis(); long endTime = System.currentTimeMillis();
sysLog.setCostTime((endTime - startTime) + "ms"); sysLog.setCostTime((endTime - TIME_THREADLOCAL.get()) + "ms");
sysLog.setPath(requestPath); sysLog.setPath(requestPath);
sysLog.setTitle(title); sysLog.setTitle(title);
sysLog.setAction(action); sysLog.setAction(action);

Loading…
Cancel
Save