Ver Fonte

系统事件

李进 há 2 anos atrás
pai
commit
c2c2cfcffd

+ 37 - 0
sys-sdk/src/main/java/com/xy/annotation/LogEnvents.java

@@ -0,0 +1,37 @@
+package com.xy.annotation;
+
+import com.xy.enums.LogEnum;
+
+import java.lang.annotation.*;
+
+/**
+ * 系统事件
+ *
+ * @author lijin
+ */
+@Target({ElementType.METHOD})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface LogEnvents {
+
+    /**
+     * 事件名称
+     *
+     * @return
+     */
+    String eventName() default "异常";
+
+    /**
+     * 事件等级
+     *
+     * @return
+     */
+    LogEnum.EventLevel eventLevel() default LogEnum.EventLevel.V1;
+
+    /**
+     * 事件描述
+     *
+     * @return
+     */
+    String eventDescript() default "";
+}

+ 38 - 1
sys-sdk/src/main/java/com/xy/annotation/aspet/LogAspet.java

@@ -2,6 +2,7 @@ package com.xy.annotation.aspet;
 
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.date.TimeInterval;
+import com.xy.annotation.LogEnvents;
 import com.xy.annotation.LogOperate;
 import com.xy.collections.list.JArrayList;
 import com.xy.collections.list.JList;
@@ -48,7 +49,7 @@ public class LogAspet {
      * @throws Throwable
      */
     @Around("@annotation(logOperate)")
-    public Object LogOperate(ProceedingJoinPoint joinPoint, LogOperate logOperate) throws Throwable {
+    public Object logOperate(ProceedingJoinPoint joinPoint, LogOperate logOperate) throws Throwable {
         TimeInterval timeInterval = DateUtil.timer();
         LogEnum.LogType logType = logOperate.logType();
         LogEnum.OptType optType = logOperate.optType();
@@ -85,6 +86,42 @@ public class LogAspet {
         return result;
     }
 
+    /**
+     * 系统事件
+     *
+     * @param joinPoint
+     * @param logEnvents
+     * @return
+     * @throws Throwable
+     */
+    @Around("@annotation(logEnvents)")
+    public Object logEnvents(ProceedingJoinPoint joinPoint, LogEnvents logEnvents) throws Throwable {
+        //参数
+        Object[] args = joinPoint.getArgs();
+        Object result;
+        try {
+            result = joinPoint.proceed(args);
+        } catch (Exception e) {
+            String eventName = logEnvents.eventName();
+            String eventDescript = logEnvents.eventDescript();
+            LogEnum.EventLevel eventLevel = logEnvents.eventLevel();
+            if (!Emptys.check(eventDescript)) {
+                String api = getApi(joinPoint);
+                Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
+                ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
+                if (apiOperation != null) {
+                    String value = apiOperation.value();
+                    eventDescript = api + "-" + value + "------" + e.getMessage();
+                }
+            }
+            if (Emptys.check(eventDescript)) {
+                LogUtils.envents(eventName, eventLevel, eventDescript);
+            }
+            throw e;
+        }
+        return result;
+    }
+
     private String getApi(ProceedingJoinPoint joinPoint) {
         Class<?> classz = joinPoint.getTarget().getClass();
         Api api = classz.getAnnotation(Api.class);