REM:
1. 增加接口日志打印信息
2. 增加鹏信设备接口请求,响应打印日志
This commit is contained in:
HuangXin 2020-10-13 18:10:14 +08:00
parent abe9ca7018
commit 4056250729
2 changed files with 135 additions and 1 deletions

View File

@ -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 <huangxin@cmhi.chinamoblie.com>
*/
@Component
@Aspect
@Slf4j
public class RestfulLog {
/**
* The Request map.
*/
private final HashMap<Integer, String> 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());
}
}
}
}

View File

@ -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<ProtocolRespDTO<T>>() {
@Override