Explorar el Código

优化请求日志

zhouhao hace 8 años
padre
commit
5170c3c52c

+ 13 - 0
hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/SimpleFiledScopeDataAccessConfig.java

@@ -17,6 +17,19 @@ public class SimpleFiledScopeDataAccessConfig extends AbstractDataAccessConfig i
 
     private String field;
 
+    public SimpleFiledScopeDataAccessConfig() {
+    }
+
+    public SimpleFiledScopeDataAccessConfig(String field, Set<Object> scope) {
+        this.scope = scope;
+        this.field = field;
+    }
+
+    public SimpleFiledScopeDataAccessConfig(String field, String scopeType) {
+        this.scopeType = scopeType;
+        this.field = field;
+    }
+
     @Override
     public String getScopeType() {
         return scopeType;

+ 17 - 1
hsweb-examples/hsweb-examples-simple/src/main/java/org/hswebframework/web/example/simple/SpringBootExample.java

@@ -56,6 +56,7 @@ import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.EnableAspectJAutoProxy;
 import org.springframework.jdbc.datasource.DataSourceUtils;
+import org.springframework.web.multipart.MultipartFile;
 import springfox.documentation.builders.ApiInfoBuilder;
 import springfox.documentation.builders.PathSelectors;
 import springfox.documentation.builders.RequestHandlerSelectors;
@@ -64,14 +65,19 @@ import springfox.documentation.spi.DocumentationType;
 import springfox.documentation.spring.web.plugins.Docket;
 import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
 import javax.sql.DataSource;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.stream.Stream;
 
 /**
  * TODO 完成注释
@@ -88,7 +94,17 @@ public class SpringBootExample implements CommandLineRunner {
 
     @Bean
     public AccessLoggerListener accessLoggerListener() {
-        return loggerInfo -> System.out.println("有请求啦:" + loggerInfo.getAction());
+        Class excludes[] = {
+                ServletRequest.class,
+                ServletResponse.class,
+                InputStream.class,
+                OutputStream.class,
+                MultipartFile.class
+        };
+        return loggerInfo -> System.out.println("有请求啦:" + JSON.toJSONString(loggerInfo.toSimpleMap(obj -> {
+            if (Stream.of(excludes).anyMatch(aClass -> aClass.isInstance(obj))) return obj.getClass().getName();
+            return JSON.toJSONString(obj);
+        })));
     }
 
     @Bean

+ 54 - 2
hsweb-logging/hsweb-access-logging-api/src/main/java/org/hswebframework/web/logging/AccessLoggerInfo.java

@@ -1,16 +1,23 @@
 package org.hswebframework.web.logging;
 
+import java.io.PrintWriter;
 import java.io.Serializable;
+import java.io.StringWriter;
 import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.StringJoiner;
+import java.util.function.Function;
 
 /**
- * 访问日志信息
+ * 访问日志信息,此对象包含了被拦截的方法和类信息,如果要对此对象进行序列化,请自行转换为想要的格式.
+ * 或者调用{@link this#toSimpleMap}获取可序列化的map格式日志信息
  *
  * @author zhouhao
  * @since 3.0
  */
-public class AccessLoggerInfo implements Serializable {
+public class AccessLoggerInfo {
 
     /**
      * 访问的操作
@@ -189,4 +196,49 @@ public class AccessLoggerInfo implements Serializable {
     public void setException(Throwable exception) {
         this.exception = exception;
     }
+
+    public Map<String, Object> toSimpleMap(Function<Object, Serializable> noSerialExchange) {
+        Map<String, Object> map = new HashMap<>(16);
+        map.put("action", action);
+        map.put("describe", describe);
+        if (method != null) {
+            StringJoiner methodAppender = new StringJoiner(",", method.getName().concat("("), ")");
+            String[] parameterNames = parameters.keySet().toArray(new String[parameters.size()]);
+            Class[] parameterTypes = method.getParameterTypes();
+
+            for (int i = 0; i < parameterTypes.length; i++) {
+                methodAppender.add(parameterTypes[i].getSimpleName().concat(" ").concat(parameterNames.length >= i ? parameterNames[i] : ("arg" + i)));
+            }
+            map.put("method", methodAppender.toString());
+        }
+        map.put("target", target != null ? target.getName() : "");
+        Map<String, Object> newParameter = new LinkedHashMap<>(parameters);
+        newParameter.entrySet().forEach(entry -> {
+            if (entry.getValue() != null && !(entry.getValue() instanceof Serializable)) {
+                entry.setValue(noSerialExchange.apply(entry.getValue()));
+            }
+        });
+
+        map.put("parameters", newParameter);
+        map.put("httpHeaders", httpHeaders);
+        map.put("httpMethod", httpMethod);
+        map.put("ip", ip);
+        map.put("url", url);
+        if (response instanceof Serializable) {
+            map.put("response", response);
+        } else {
+            map.put("response", noSerialExchange.apply(response));
+        }
+        map.put("requestTime", requestTime);
+        map.put("responseTime", responseTime);
+        map.put("useTime", responseTime - requestTime);
+        if (exception != null) {
+            StringWriter writer = new StringWriter();
+            exception.printStackTrace(new PrintWriter(writer));
+            map.put("exception", writer.toString());
+        }
+
+
+        return map;
+    }
 }