Browse Source

操作日志代码优化

yucheng 3 years ago
parent
commit
4f88edc7e2

+ 28 - 19
dmp-server/src/main/java/com/persagy/aspects/RequestLogAspect.java

@@ -1,9 +1,11 @@
 package com.persagy.aspects;
 
+import cn.hutool.core.util.ArrayUtil;
 import cn.hutool.core.util.IdUtil;
+import cn.hutool.core.util.StrUtil;
 import cn.hutool.extra.servlet.ServletUtil;
 import com.persagy.dmp.common.model.response.CommonResult;
-import com.persagy.utils.JacksonMapper;
+import com.persagy.dmp.common.utils.JsonHelper;
 import lombok.extern.slf4j.Slf4j;
 import org.aspectj.lang.JoinPoint;
 import org.aspectj.lang.annotation.*;
@@ -48,25 +50,27 @@ public class RequestLogAspect {
 		LogData data = initFlag(request);
 		dataStorage.set(data);
 		log.info("{} REQUEST URL      : {} {} {} {}", data.intFlag, data.method, data.url, data.remoteIp, data.remotePort);
-		log.info("{} REQUEST ARGS     : {} ", data.intFlag, JacksonMapper.toSimpleJson(prepare(joinPoint)));
+		log.info("{} REQUEST ARGS     : {} ", data.intFlag, JsonHelper.toJsonStrQuietly(prepare(joinPoint)));
 	}
 
+	/**
+	 * 准备数据
+	 * @param joinPoint
+	 * @return
+	 */
 	public static List<Object> prepare(JoinPoint joinPoint) {
 		Object[] args = joinPoint.getArgs();
 		List<Object> list = new LinkedList<>();
-		if (args != null && args.length > 0) {
-			for (Object arg : args) {
-				if (arg instanceof ServletRequest) {
-					continue;
-				}
-				if (arg instanceof ServletResponse) {
-					continue;
-				}
-				if (arg instanceof MultipartFile) {
-					continue;
-				}
-				list.add(arg);
+		if(ArrayUtil.isEmpty(args)) {
+			return list;
+		}
+		for (Object arg : args) {
+			if (arg instanceof ServletRequest ||
+					arg instanceof ServletResponse ||
+					arg instanceof MultipartFile) {
+				continue;
 			}
+			list.add(arg);
 		}
 		return list;
 	}
@@ -74,15 +78,15 @@ public class RequestLogAspect {
 	@AfterReturning(returning = "ret", pointcut = "controllerPoint()")
 	public void doAfterReturning(Object ret) {
 		LogData data = dataStorage.get();
+		String result = null;
 		if (ret != null) {
 			if (ret instanceof CommonResult) {
-				log.info("{} REQUEST DURATION : {} {} {}", data.intFlag, System.currentTimeMillis() - data.timestamp, ((CommonResult) ret).getResult(), data.url);
+				result = ((CommonResult) ret).getResult();
 			} else {
-				log.info("{} REQUEST DURATION : {} {} {}", data.intFlag, System.currentTimeMillis() - data.timestamp, ret.getClass().getName(), data.url);
+				result = ret.getClass().getName();
 			}
-		} else {
-			log.info("{} REQUEST DURATION : {} {} {}", data.intFlag, System.currentTimeMillis() - data.timestamp, "null", data.url);
 		}
+		log.info("{} REQUEST DURATION : {} {} {}", data.intFlag, System.currentTimeMillis() - data.timestamp, result, data.url);
 		dataStorage.remove();
 	}
 
@@ -93,6 +97,11 @@ public class RequestLogAspect {
 		dataStorage.remove();
 	}
 
+	/**
+	 * 初始化日志记录
+	 * @param request
+	 * @return
+	 */
 	private static LogData initFlag(HttpServletRequest request) {
 		LogData data = new LogData();
 		// 请求IP
@@ -103,7 +112,7 @@ public class RequestLogAspect {
 		data.method = request.getMethod();
 		String requestURI = request.getRequestURI();
 		String queryString = request.getQueryString();
-		data.url = requestURI + (queryString == null ? "" : "?" + queryString);
+		data.url = StrUtil.isBlank(queryString) ? requestURI : StrUtil.concat(true, requestURI, "?", queryString);
 		data.intFlag = IdUtil.fastSimpleUUID();
 		dataStorage.set(data);
 		return data;

+ 0 - 222
dmp-server/src/main/java/com/persagy/utils/JacksonMapper.java

@@ -1,222 +0,0 @@
-package com.persagy.utils;
-
-import com.fasterxml.jackson.annotation.JsonInclude;
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.core.type.TypeReference;
-import com.fasterxml.jackson.databind.*;
-import com.fasterxml.jackson.databind.node.ArrayNode;
-import com.fasterxml.jackson.databind.node.JsonNodeType;
-import com.fasterxml.jackson.databind.node.ObjectNode;
-import lombok.extern.slf4j.Slf4j;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.IOException;
-
-/**
- * @author: yaoll
- * @date: 2020-09-04
- * @verison: 1.0
- */
-@Slf4j
-public class JacksonMapper {
-
-	public static final ObjectMapper nonEmptyMapper;
-	public static final ObjectMapper nonDefaultMapper;
-	public static final ObjectMapper nonEmptyFormatMapper;
-
-	static {
-		nonEmptyMapper = new ObjectMapper();
-		nonEmptyMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
-		nonEmptyMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
-		nonEmptyMapper.disable(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES);
-
-		nonDefaultMapper = new ObjectMapper();
-		nonDefaultMapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
-		nonDefaultMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
-		nonDefaultMapper.disable(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES);
-
-		nonEmptyFormatMapper = new ObjectMapper();
-		nonEmptyFormatMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
-		nonEmptyFormatMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
-		nonEmptyFormatMapper.disable(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES);
-		nonEmptyFormatMapper.enable(SerializationFeature.INDENT_OUTPUT);
-	}
-
-	private static boolean isEmpty(final CharSequence cs) {
-		return cs == null || cs.length() == 0;
-	}
-
-	public static <T> T toObject(String jsonString, Class<T> clazz) {
-		if (!isEmpty(jsonString)) {
-			try {
-				return nonEmptyMapper.readValue(jsonString, clazz);
-			} catch (IOException e) {
-				log.warn("parse json string to {} error: {}", clazz.getName(), jsonString, e);
-			}
-		}
-		return null;
-	}
-
-	public static <T> T toObject(String jsonString, TypeReference<T> typeRef) {
-		if (!isEmpty(jsonString)) {
-			try {
-				return nonEmptyMapper.readValue(jsonString, typeRef);
-			} catch (IOException e) {
-				log.warn("parse json string to {} error: {}", typeRef.toString(), jsonString, e);
-			}
-		}
-		return null;
-	}
-
-	public static <T> T toObject(String jsonString, JavaType javaType) {
-		if (!isEmpty(jsonString)) {
-			try {
-				return (T) nonEmptyMapper.readValue(jsonString, javaType);
-			} catch (IOException e) {
-				log.warn("parse json string to {} error: {}", javaType.toString(), jsonString, e);
-			}
-		}
-		return null;
-	}
-
-	public static <T> T toObject(String jsonString, Class cla, Class... clb) {
-		if (!isEmpty(jsonString)) {
-			try {
-				JavaType javaType = nonEmptyMapper.getTypeFactory().constructParametricType(cla, clb);
-				return (T) nonEmptyMapper.readValue(jsonString, javaType);
-			} catch (IOException e) {
-				log.warn("parse json string to {} error: {}", cla.getName(), jsonString, e);
-			}
-		}
-		return null;
-	}
-
-	public static JavaType constructParametricType(Class<?> parametrized, Class<?>... parameterClasses) {
-		return nonEmptyMapper.getTypeFactory().constructParametricType(parametrized, parameterClasses);
-	}
-
-	public static JavaType constructParametricType(Class<?> rawType, JavaType... parameterTypes) {
-		return nonEmptyMapper.getTypeFactory().constructParametricType(rawType, parameterTypes);
-	}
-
-	public static String toSimpleJson(Object object) {
-		try {
-			return nonEmptyMapper.writeValueAsString(object);
-		} catch (JsonProcessingException e) {
-			log.error("write to json string error:" + object, e);
-		}
-		return null;
-	}
-
-	public static String toFormatJson(Object object) {
-		try {
-			return nonEmptyFormatMapper.writeValueAsString(object);
-		} catch (JsonProcessingException e) {
-			log.error("write to json string error:" + object, e);
-		}
-		return null;
-	}
-
-	public static final String getString(ObjectNode obj, String key) {
-		return getString(obj, key, null);
-	}
-
-	public static final String getString(ObjectNode obj, String key, String defaultValue) {
-		if (obj == null) {
-			return defaultValue;
-		}
-		if (!obj.has(key)) {
-			return defaultValue;
-		}
-		return obj.get(key).asText();
-	}
-
-	public static final Integer getInteger(ObjectNode obj, String key) {
-		return getInteger(obj, key, null);
-	}
-
-	public static final Integer getInteger(ObjectNode obj, String key, Integer defaultValue) {
-		if (obj == null) {
-			return defaultValue;
-		}
-		if (!obj.has(key)) {
-			return defaultValue;
-		}
-		JsonNode jsonNode = obj.get(key);
-
-		return Integer.valueOf(jsonNode.asText());
-	}
-
-	public static final Double getDouble(ObjectNode obj, String key) {
-		return getDouble(obj, key, null);
-	}
-
-	public static final Double getDouble(ObjectNode obj, String key, Double defaultValue) {
-		if (obj == null) {
-			return defaultValue;
-		}
-		if (!obj.has(key)) {
-			return defaultValue;
-		}
-		return Double.valueOf(obj.get(key).asText());
-	}
-
-	public static final Boolean getBoolean(ObjectNode obj, String key) {
-		return getBoolean(obj, key, null);
-	}
-
-	public static final Boolean getBoolean(ObjectNode obj, String key, Boolean defaultValue) {
-		if (obj == null) {
-			return defaultValue;
-		}
-		if (!obj.has(key)) {
-			return defaultValue;
-		}
-		return Boolean.valueOf(obj.get(key).toString());
-	}
-
-	public static final ObjectNode getObject(ObjectNode obj, String key) {
-		return getObject(obj, key, null);
-	}
-
-	public static final ObjectNode getObject(ObjectNode obj, String key, ObjectNode defaultValue) {
-		if (obj == null) {
-			return defaultValue;
-		}
-		if (!obj.has(key)) {
-			return defaultValue;
-		}
-		JsonNode jsonNode = obj.get(key);
-		JsonNodeType nodeType = jsonNode.getNodeType();
-		if (nodeType == JsonNodeType.STRING) {
-			return toObject(jsonNode.asText(), ObjectNode.class);
-		} else if (nodeType == JsonNodeType.OBJECT) {
-			return (ObjectNode) jsonNode;
-		}
-		return defaultValue;
-	}
-
-
-	public static final ArrayNode getArray(ObjectNode obj, String key) {
-		return getArray(obj, key, null);
-	}
-
-	public static final ArrayNode getArray(ObjectNode obj, String key, ArrayNode defaultValue) {
-		if (obj == null) {
-			return defaultValue;
-		}
-		if (!obj.has(key)) {
-			return defaultValue;
-		}
-		JsonNode jsonNode = obj.get(key);
-		JsonNodeType nodeType = jsonNode.getNodeType();
-		if (nodeType == JsonNodeType.STRING) {
-			return toObject(jsonNode.asText(), ArrayNode.class);
-		} else if (nodeType == JsonNodeType.ARRAY) {
-			return (ArrayNode) jsonNode;
-		}
-		return defaultValue;
-	}
-
-}