瀏覽代碼

优化访问日志逻辑

zhou-hao 7 年之前
父節點
當前提交
2e61d0381d

+ 5 - 0
hsweb-logging/hsweb-access-logging-aop/pom.xml

@@ -44,6 +44,11 @@
             <groupId>org.springframework</groupId>
             <artifactId>spring-webmvc</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.hswebframework.web</groupId>
+            <artifactId>hsweb-core</artifactId>
+            <version>${project.version}</version>
+        </dependency>
         <dependency>
             <groupId>javax.servlet</groupId>
             <artifactId>servlet-api</artifactId>

+ 11 - 1
hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/loggin/aop/AopAccessLoggerSupport.java

@@ -4,6 +4,7 @@ import org.aopalliance.intercept.MethodInterceptor;
 import org.hswebframework.web.AopUtils;
 import org.hswebframework.web.WebUtil;
 import org.hswebframework.web.boost.aop.context.MethodInterceptorHolder;
+import org.hswebframework.web.id.IDGenerator;
 import org.hswebframework.web.logging.AccessLogger;
 import org.hswebframework.web.logging.AccessLoggerInfo;
 import org.hswebframework.web.logging.AccessLoggerListener;
@@ -13,6 +14,7 @@ import org.springframework.core.Ordered;
 import org.springframework.core.annotation.AnnotationUtils;
 import org.springframework.stereotype.Controller;
 import org.springframework.util.ClassUtils;
+import org.springframework.util.SimpleIdGenerator;
 import org.springframework.web.bind.annotation.RequestMapping;
 
 import javax.servlet.http.HttpServletRequest;
@@ -20,6 +22,7 @@ import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Objects;
+import java.util.UUID;
 import java.util.stream.Stream;
 
 /**
@@ -50,6 +53,7 @@ public class AopAccessLoggerSupport extends StaticMethodMatcherPointcutAdvisor {
             AccessLoggerInfo info = createLogger(methodInterceptorHolder);
             Object response;
             try {
+                listeners.forEach(listener -> listener.onLogBefore(info));
                 response = methodInvocation.proceed();
                 info.setResponse(response);
                 info.setResponseTime(System.currentTimeMillis());
@@ -66,6 +70,8 @@ public class AopAccessLoggerSupport extends StaticMethodMatcherPointcutAdvisor {
 
     protected AccessLoggerInfo createLogger(MethodInterceptorHolder holder) {
         AccessLoggerInfo info = new AccessLoggerInfo();
+        info.setId(IDGenerator.MD5.generate());
+
         info.setRequestTime(System.currentTimeMillis());
 
 
@@ -101,9 +107,13 @@ public class AopAccessLoggerSupport extends StaticMethodMatcherPointcutAdvisor {
 
     @Override
     public boolean matches(Method method, Class<?> aClass) {
+        AccessLogger ann = AopUtils.findAnnotation(aClass, method, AccessLogger.class);
+        if(ann!=null&&ann.ignore()){
+            return false;
+        }
         RequestMapping mapping= AopUtils.findAnnotation(aClass,method, RequestMapping.class);
         return mapping!=null;
-//        AccessLogger ann = AopUtils.findAnnotation(aClass, method, AccessLogger.class);
+
 //        //注解了并且未取消
 //        return null != ann && !ann.ignore();
     }

+ 0 - 4
hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/loggin/aop/SwaggerAccessLoggerParser.java

@@ -13,10 +13,6 @@ import java.lang.reflect.Method;
 public class SwaggerAccessLoggerParser implements AccessLoggerParser {
     @Override
     public boolean support(Class clazz, Method method) {
-        AccessLogger ann = AopUtils.findAnnotation(clazz, method, AccessLogger.class);
-        if (null != ann && ann.ignore()) {
-            return false;
-        }
 
         Api api = AnnotationUtils.findAnnotation(clazz, Api.class);
         ApiOperation operation = AnnotationUtils.findAnnotation(method, ApiOperation.class);

+ 14 - 0
hsweb-logging/hsweb-access-logging-api/src/main/java/org/hswebframework/web/logging/AccessLoggerInfo.java

@@ -18,6 +18,11 @@ import java.util.function.Function;
  */
 public class AccessLoggerInfo {
 
+    /**
+     * 日志id
+     */
+    private String id;
+
     /**
      * 访问的操作
      *
@@ -229,6 +234,7 @@ public class AccessLoggerInfo {
         map.put("response", objectFilter.apply(response));
         map.put("requestTime", requestTime);
         map.put("responseTime", responseTime);
+        map.put("id",id);
         map.put("useTime", responseTime - requestTime);
         if (exception != null) {
             StringWriter writer = new StringWriter();
@@ -237,4 +243,12 @@ public class AccessLoggerInfo {
         }
         return map;
     }
+
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+    }
 }

+ 2 - 0
hsweb-logging/hsweb-access-logging-api/src/main/java/org/hswebframework/web/logging/AccessLoggerListener.java

@@ -14,4 +14,6 @@ public interface AccessLoggerListener {
      * @param loggerInfo 产生的日志信息
      */
     void onLogger(AccessLoggerInfo loggerInfo);
+
+    default void onLogBefore(AccessLoggerInfo loggerInfo){}
 }