Sfoglia il codice sorgente

优化访问日志

zhou-hao 7 anni fa
parent
commit
bfcab06195

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

@@ -36,6 +36,14 @@
             <groupId>org.springframework</groupId>
             <artifactId>spring-aop</artifactId>
         </dependency>
+        <dependency>
+            <groupId>io.swagger</groupId>
+            <artifactId>swagger-annotations</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-webmvc</artifactId>
+        </dependency>
         <dependency>
             <groupId>javax.servlet</groupId>
             <artifactId>servlet-api</artifactId>

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

@@ -0,0 +1,13 @@
+package org.hswebframework.web.loggin.aop;
+
+
+import org.hswebframework.web.boost.aop.context.MethodInterceptorHolder;
+import org.hswebframework.web.logging.LoggerDefine;
+
+import java.lang.reflect.Method;
+
+public interface AccessLoggerParser {
+    boolean support(Class clazz, Method method);
+
+    LoggerDefine parse(MethodInterceptorHolder holder);
+}

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

@@ -7,8 +7,13 @@ import org.hswebframework.web.boost.aop.context.MethodInterceptorHolder;
 import org.hswebframework.web.logging.AccessLogger;
 import org.hswebframework.web.logging.AccessLoggerInfo;
 import org.hswebframework.web.logging.AccessLoggerListener;
+import org.hswebframework.web.logging.LoggerDefine;
 import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;
 import org.springframework.core.Ordered;
+import org.springframework.core.annotation.AnnotationUtils;
+import org.springframework.stereotype.Controller;
+import org.springframework.util.ClassUtils;
+import org.springframework.web.bind.annotation.RequestMapping;
 
 import javax.servlet.http.HttpServletRequest;
 import java.lang.reflect.Method;
@@ -27,11 +32,18 @@ public class AopAccessLoggerSupport extends StaticMethodMatcherPointcutAdvisor {
 
     private final List<AccessLoggerListener> listeners = new ArrayList<>();
 
+    private final List<AccessLoggerParser> loggerParsers=new ArrayList<>();
+
     public AopAccessLoggerSupport addListener(AccessLoggerListener loggerListener) {
         listeners.add(loggerListener);
         return this;
     }
 
+    public AopAccessLoggerSupport addParser(AccessLoggerParser parser) {
+        loggerParsers.add(parser);
+        return this;
+    }
+
     public AopAccessLoggerSupport() {
         setAdvice((MethodInterceptor) methodInvocation -> {
             MethodInterceptorHolder methodInterceptorHolder = MethodInterceptorHolder.create(methodInvocation);
@@ -56,23 +68,17 @@ public class AopAccessLoggerSupport extends StaticMethodMatcherPointcutAdvisor {
         AccessLoggerInfo info = new AccessLoggerInfo();
         info.setRequestTime(System.currentTimeMillis());
 
-        AccessLogger methodAnn = holder.findMethodAnnotation(AccessLogger.class);
-        AccessLogger classAnn = holder.findClassAnnotation(AccessLogger.class);
-
-        String action = Stream.of(classAnn, methodAnn)
-                .filter(Objects::nonNull)
-                .map(AccessLogger::value)
-                .reduce((c, m) -> c.concat("-").concat(m))
-                .orElse("");
-        String describe = Stream.of(classAnn, methodAnn)
-                .filter(Objects::nonNull)
-                .map(AccessLogger::describe)
-                .flatMap(Stream::of)
-                .reduce((c, s) -> c.concat("\n").concat(s))
-                .orElse("");
-
-        info.setAction(action);
-        info.setDescribe(describe);
+
+        LoggerDefine define=loggerParsers.stream()
+                .filter(parser->parser.support(ClassUtils.getUserClass(holder.getTarget()),holder.getMethod()))
+                .findAny()
+                .map(parser->parser.parse(holder))
+                .orElse(null);
+
+        if(define!=null) {
+            info.setAction(define.getAction());
+            info.setDescribe(define.getDescribe());
+        }
         info.setParameters(holder.getArgs());
         info.setTarget(holder.getTarget().getClass());
         info.setMethod(holder.getMethod());
@@ -95,8 +101,10 @@ public class AopAccessLoggerSupport extends StaticMethodMatcherPointcutAdvisor {
 
     @Override
     public boolean matches(Method method, Class<?> aClass) {
-        AccessLogger ann = AopUtils.findAnnotation(aClass, method, AccessLogger.class);
-        //注解了并且未取消
-        return null != ann && !ann.ignore();
+        RequestMapping mapping= AopUtils.findAnnotation(aClass,method, RequestMapping.class);
+        return mapping!=null;
+//        AccessLogger ann = AopUtils.findAnnotation(aClass, method, AccessLogger.class);
+//        //注解了并且未取消
+//        return null != ann && !ann.ignore();
     }
 }

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

@@ -25,6 +25,16 @@ public class AopAccessLoggerSupportAutoConfiguration {
         return new AopAccessLoggerSupport();
     }
 
+    @Bean
+    public DefaultAccessLoggerParser defaultAccessLoggerParser(){
+        return new DefaultAccessLoggerParser();
+    }
+
+    @Bean
+    @ConditionalOnClass(name = "io.swagger.annotations.Api")
+    public SwaggerAccessLoggerParser swaggerAccessLoggerParser(){
+        return new SwaggerAccessLoggerParser();
+    }
     @Bean
     public ListenerProcessor listenerProcessor() {
         return new ListenerProcessor();
@@ -44,6 +54,8 @@ public class AopAccessLoggerSupportAutoConfiguration {
         public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
             if (bean instanceof AccessLoggerListener) {
                 aopAccessLoggerSupport.addListener(((AccessLoggerListener) bean));
+            }  if (bean instanceof AccessLoggerParser) {
+                aopAccessLoggerSupport.addParser(((AccessLoggerParser) bean));
             }
             return bean;
         }

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

@@ -0,0 +1,39 @@
+package org.hswebframework.web.loggin.aop;
+
+
+import org.hswebframework.web.AopUtils;
+import org.hswebframework.web.boost.aop.context.MethodInterceptorHolder;
+import org.hswebframework.web.logging.AccessLogger;
+import org.hswebframework.web.logging.LoggerDefine;
+
+import java.lang.reflect.Method;
+import java.util.Objects;
+import java.util.stream.Stream;
+
+
+public class DefaultAccessLoggerParser implements AccessLoggerParser {
+    @Override
+    public boolean support(Class clazz, Method method) {
+        AccessLogger ann = AopUtils.findAnnotation(clazz, method, AccessLogger.class);
+        //注解了并且未取消
+        return null != ann && !ann.ignore();
+    }
+
+    @Override
+    public LoggerDefine parse(MethodInterceptorHolder holder) {
+        AccessLogger methodAnn = holder.findMethodAnnotation(AccessLogger.class);
+        AccessLogger classAnn = holder.findClassAnnotation(AccessLogger.class);
+        String action = Stream.of(classAnn, methodAnn)
+                .filter(Objects::nonNull)
+                .map(AccessLogger::value)
+                .reduce((c, m) -> c.concat("-").concat(m))
+                .orElse("");
+        String describe = Stream.of(classAnn, methodAnn)
+                .filter(Objects::nonNull)
+                .map(AccessLogger::describe)
+                .flatMap(Stream::of)
+                .reduce((c, s) -> c.concat("\n").concat(s))
+                .orElse("");
+        return new LoggerDefine(action,describe);
+    }
+}

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

@@ -0,0 +1,36 @@
+package org.hswebframework.web.loggin.aop;
+
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.hswebframework.web.AopUtils;
+import org.hswebframework.web.boost.aop.context.MethodInterceptorHolder;
+import org.hswebframework.web.logging.AccessLogger;
+import org.hswebframework.web.logging.LoggerDefine;
+import org.springframework.core.annotation.AnnotationUtils;
+
+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);
+
+        return api != null || operation != null;
+    }
+
+    @Override
+    public LoggerDefine parse(MethodInterceptorHolder holder) {
+        ApiOperation operation = holder.findAnnotation(ApiOperation.class);
+        String action = "";
+        if (null != operation) {
+            action = operation.value();
+        }
+        return new LoggerDefine(action, "");
+    }
+}

+ 30 - 0
hsweb-logging/hsweb-access-logging-api/src/main/java/org/hswebframework/web/logging/LoggerDefine.java

@@ -0,0 +1,30 @@
+package org.hswebframework.web.logging;
+
+
+public class LoggerDefine {
+    private String action;
+
+    private String describe;
+
+    public LoggerDefine(String action,String describe){
+        this.action=action;
+        this.describe=describe;
+    }
+
+    public String getDescribe() {
+        return describe;
+    }
+
+    public void setAction(String action) {
+        this.action = action;
+    }
+
+    public String getAction() {
+        return action;
+    }
+
+    public void setDescribe(String describe) {
+        this.describe = describe;
+    }
+}
+