lwhhszx 2 lat temu
rodzic
commit
89f2d3e66f

+ 60 - 0
PCS/src/main/java/cn/cslg/permission/common/core/annotation/LoggerAspect.java

@@ -0,0 +1,60 @@
+package cn.cslg.permission.common.core.annotation;
+
+
+import cn.cslg.permission.common.utils.JsonUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.AfterThrowing;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Pointcut;
+import org.springframework.core.annotation.Order;
+import org.springframework.stereotype.Component;
+
+@Order(3)
+@Aspect
+@Component
+@Slf4j
+public class LoggerAspect {
+    /**
+     * 定义切点
+     */
+    @Pointcut("execution( * cn.cslg.permission.controller..*.*(..))")
+    public void annotationPointcut() {
+
+    }
+    /**
+     * @param joinPoint 当前执行的方法
+     */
+    @Around("annotationPointcut()")
+    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
+
+        //判断通过
+        return joinPoint.proceed();
+    }
+
+    @AfterThrowing(pointcut = "annotationPointcut()",throwing = "e")
+    public void doAfterThrowing(JoinPoint joinPoint, Throwable e){
+        //获取用户请求方法的参数并序列化为JSON格式字符串
+        String params = "";
+        if (joinPoint.getArgs()!=null&&joinPoint.getArgs().length>0){
+            for (int i = 0; i < joinPoint.getArgs().length; i++) {
+                params+= JsonUtils.objectToJson(joinPoint.getArgs()[i])+";";
+            }
+        }
+        try{
+            String str1 ="异常代码:" + e.getClass().getName();
+            String str2 ="异常信息:" + e.getMessage();
+            String str3="异常方法:" + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()");
+            String str4="请求参数:" + params;
+   log.error(str1+"\r"+str2+"\r"+str3+"\r"+str4);
+        }catch (Exception ex){
+            //记录本地异常日志
+            log.error("==异常通知异常==");
+            log.error("异常信息:{}", ex.getMessage());
+        }
+    }
+
+}
+