From 40562507295463e3807bc77c9880f2c2a4024002 Mon Sep 17 00:00:00 2001 From: HuangXin Date: Tue, 13 Oct 2020 18:10:14 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E5=A2=9E=E5=8A=A0=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=97=A5=E5=BF=97=E6=89=93=E5=8D=B0=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=202.=20=E5=A2=9E=E5=8A=A0=E9=B9=8F=E4=BF=A1=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=AF=B7=E6=B1=82=EF=BC=8C=E5=93=8D=E5=BA=94?= =?UTF-8?q?=E6=89=93=E5=8D=B0=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/logging/RestfulLog.java | 128 ++++++++++++++++++ .../com/dispose/restful/RestfulInterface.java | 8 +- 2 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/dispose/controller/logging/RestfulLog.java diff --git a/src/main/java/com/dispose/controller/logging/RestfulLog.java b/src/main/java/com/dispose/controller/logging/RestfulLog.java new file mode 100644 index 00000000..73d01caf --- /dev/null +++ b/src/main/java/com/dispose/controller/logging/RestfulLog.java @@ -0,0 +1,128 @@ +package com.dispose.controller.logging; + +import com.dispose.common.ConstValue; +import com.dispose.pojo.dto.protocol.base.ProtocolReqDTO; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.annotation.AfterReturning; +import org.aspectj.lang.annotation.AfterThrowing; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Pointcut; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import javax.servlet.http.HttpServletRequest; +import java.util.HashMap; + +/** + * The type Restful log. + * + * @author + */ +@Component +@Aspect +@Slf4j +public class RestfulLog { + + /** + * The Request map. + */ + private final HashMap requestMap = new HashMap<>(); + + /** + * The Object mapper. + */ + private final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + + /** + * Dispose project exception. + */ + @Pointcut("execution(* com.dispose.controller..*.*(..))") + public void disposeProjectException() { + } + + /** + * Restful request before. + * + * @param jp the jp + * @throws JsonProcessingException the json processing exception + */ + @Before(value = "within(com.dispose.controller.*)") + public void restfulRequestBefore(JoinPoint jp) throws JsonProcessingException { + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + + if (attributes != null) { + for (Object obj : jp.getArgs()) { + if (obj instanceof ProtocolReqDTO) { + requestMap.put(attributes.hashCode(), OBJECT_MAPPER.writeValueAsString(obj)); + break; + } + } + } + } + + /** + * Restful response after. + * + * @param ret the ret + * @throws JsonProcessingException the json processing exception + */ + @AfterReturning(value = "within(com.dispose.controller.*)", returning = "ret") + public void restfulResponseAfter(Object ret) throws JsonProcessingException { + + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + + if (attributes != null) { + HttpServletRequest request = attributes.getRequest(); + String reqType = request.getMethod(); + String reqPath = request.getRequestURI(); + String reqIp = request.getRemoteAddr(); + String reqToken = request.getHeader("Authorization"); + + if (reqToken != null && reqToken.length() > 0) { + reqToken = reqToken.replace(ConstValue.STRING_HTTP_AUTH_HEAD, ""); + } + + if (requestMap.containsKey(attributes.hashCode())) { + log.debug("Interface [{}] request <{}> from {}, token = <{}>\n" + + "+++ Request: {}\n" + + "--- Response: {}", + reqType, reqPath, reqIp, reqToken, requestMap.get(attributes.hashCode()), + OBJECT_MAPPER.writeValueAsString(ret)); + } + } + } + + /** + * Restful exception. + * + * @param ex the ex + */ + @AfterThrowing(pointcut = "disposeProjectException()", throwing = "ex") + public void restfulException(Exception ex) { + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + + if (attributes != null) { + HttpServletRequest request = attributes.getRequest(); + String reqType = request.getMethod(); + String reqPath = request.getRequestURI(); + String reqIp = request.getRemoteAddr(); + String reqToken = request.getHeader("Authorization"); + + if (reqToken != null && reqToken.length() > 0) { + reqToken = reqToken.replace(ConstValue.STRING_HTTP_AUTH_HEAD, ""); + } + + if (requestMap.containsKey(attributes.hashCode())) { + log.debug("Interface [{}] request <{}> from {}, token = <{}>\n" + + "+++ Request: {}\n" + + "--- Response Exception: {}", + reqType, reqPath, reqIp, reqToken, requestMap.get(attributes.hashCode()), ex.getMessage()); + } + } + } +} diff --git a/src/main/java/com/dispose/restful/RestfulInterface.java b/src/main/java/com/dispose/restful/RestfulInterface.java index b7cf1c96..a40e0322 100644 --- a/src/main/java/com/dispose/restful/RestfulInterface.java +++ b/src/main/java/com/dispose/restful/RestfulInterface.java @@ -114,12 +114,16 @@ public class RestfulInterface { httpHeadMap.put(String.valueOf(Header.AUTHORIZATION), ConstValue.STRING_HTTP_AUTH_HEAD + token); } + String reqJson = OBJECT_MAPPER.writeValueAsString(obj); + + log.debug("PengXin request: {}, {}: {}", url, token, reqJson); + switch (reqType) { case GET: svrResp = getJson(url, httpHeadMap); break; case POST: - svrResp = postJson(url, httpHeadMap, OBJECT_MAPPER.writeValueAsString(obj)); + svrResp = postJson(url, httpHeadMap, reqJson); break; default: log.error("Unknown method: {}", reqType); @@ -131,6 +135,8 @@ public class RestfulInterface { return null; } + log.debug("PengXin response: {}, {}: {}", url, token, svrResp); + return OBJECT_MAPPER.readValue(svrResp, new TypeReference>() { @Override