|
@@ -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;
|
|
|
+ }
|
|
|
}
|