|
@@ -0,0 +1,136 @@
|
|
|
+package com.xt.js.gkaq.common.log.aspect;
|
|
|
+
|
|
|
+import java.lang.reflect.Method;
|
|
|
+
|
|
|
+import org.aspectj.lang.JoinPoint;
|
|
|
+import org.aspectj.lang.Signature;
|
|
|
+import org.aspectj.lang.annotation.AfterThrowing;
|
|
|
+import org.aspectj.lang.annotation.Aspect;
|
|
|
+import org.aspectj.lang.annotation.Before;
|
|
|
+import org.aspectj.lang.annotation.Pointcut;
|
|
|
+import org.aspectj.lang.reflect.MethodSignature;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import com.xt.js.gkaq.common.Constants;
|
|
|
+import com.xt.js.gkaq.common.ex.BusinessException;
|
|
|
+import com.xt.js.gkaq.common.log.annotation.SysLog;
|
|
|
+import com.xt.js.gkaq.frame.model.SysLogModel;
|
|
|
+import com.xt.js.gkaq.frame.service.SysLogService;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 日志管理切点类
|
|
|
+ */
|
|
|
+@Aspect
|
|
|
+@Component
|
|
|
+public class SysLogAspect {
|
|
|
+ @Autowired
|
|
|
+ private SysLogService sysLogService;
|
|
|
+ // 日志对象
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(SysLogAspect.class);
|
|
|
+
|
|
|
+ // 日志切入点
|
|
|
+ @Pointcut("@annotation(com.xt.js.gkaq.common.log.annotation.SysLog)")
|
|
|
+ public void sysLogPointcut() {
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 前置通知
|
|
|
+ *
|
|
|
+ * @param joinPoint
|
|
|
+ */
|
|
|
+ @Before("sysLogPointcut()")
|
|
|
+ public void doBefore(JoinPoint joinPoint) {
|
|
|
+ genSysLog(joinPoint, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ // /**
|
|
|
+ // * 后置通知
|
|
|
+ // * @param joinPoint
|
|
|
+ // */
|
|
|
+ // @AfterReturning(pointcut = "sysLogPointcut()")
|
|
|
+ // public void doAfter(JoinPoint joinPoint) {
|
|
|
+ // genSysLog(joinPoint, null);
|
|
|
+ // }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 异常通知
|
|
|
+ *
|
|
|
+ * @param joinPoint
|
|
|
+ * @param e
|
|
|
+ */
|
|
|
+ @AfterThrowing(value = "sysLogPointcut()", throwing = "e")
|
|
|
+ public void doAfter(JoinPoint joinPoint, Exception e) {
|
|
|
+ genSysLog(joinPoint, e);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成系统日志
|
|
|
+ *
|
|
|
+ * @param joinPoint
|
|
|
+ * @param ex
|
|
|
+ */
|
|
|
+ private void genSysLog(JoinPoint joinPoint, Exception ex) {
|
|
|
+ try {
|
|
|
+ // 获取方法名
|
|
|
+ String methodName = joinPoint.getSignature().getName();
|
|
|
+ // 获取目标类名
|
|
|
+ String targetName = joinPoint.getTarget().getClass().getName();
|
|
|
+ // 类路径
|
|
|
+ String classPath = targetName + "." + methodName + "()";
|
|
|
+ // 获取系统日志注解
|
|
|
+ SysLog sysLog = getSysLogAnnotation(joinPoint);
|
|
|
+ if (sysLog == null) {
|
|
|
+ logger.error("{}获取系统日志注解为空!", classPath);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ String description = sysLog.description();
|
|
|
+ String type = sysLog.type();
|
|
|
+ if (ex != null) {
|
|
|
+ if (ex instanceof BusinessException) {
|
|
|
+ type = Constants.LOG_TYPE_2;
|
|
|
+ BusinessException bex = (BusinessException) ex;
|
|
|
+ description += "[发生业务异常,异常代码:" + bex.getErrorCode() + ",异常信息:" + bex.getErrorMsg() + "]";
|
|
|
+ } else {
|
|
|
+ type = Constants.LOG_TYPE_1;
|
|
|
+ description += "[发生异常:" + ex.getMessage() + "]";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ logger.info("生成系统日志[日志类路径:{},日志类型:{},日志描述:{}]", classPath, type, description);
|
|
|
+
|
|
|
+ // 数据库日志
|
|
|
+ SysLogModel log = new SysLogModel();
|
|
|
+ log.setClassPath(classPath);
|
|
|
+ log.setType(type);
|
|
|
+ log.setMessage(description);
|
|
|
+ // 保存数据库
|
|
|
+ sysLogService.save(log);
|
|
|
+ } catch (Exception exp) {
|
|
|
+ logger.error("生成系统日志发生异常:", exp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取系统日志注解
|
|
|
+ *
|
|
|
+ * @param joinPoint
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ private static SysLog getSysLogAnnotation(JoinPoint joinPoint) throws Exception {
|
|
|
+
|
|
|
+ Signature signature = joinPoint.getSignature();
|
|
|
+ MethodSignature methodSignature = (MethodSignature) signature;
|
|
|
+ Method method = methodSignature.getMethod();
|
|
|
+
|
|
|
+ if (method != null) {
|
|
|
+ return method.getAnnotation(SysLog.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|