Commit 7c79c79e authored by 赵啸非's avatar 赵啸非

修改网关日志

parent db8a61d7
...@@ -35,6 +35,11 @@ import java.util.Map; ...@@ -35,6 +35,11 @@ import java.util.Map;
@Order(1) @Order(1)
@Profile({"default", "develop", "test"}) @Profile({"default", "develop", "test"})
public class WebLogAspect { public class WebLogAspect {
private static final String TRACE_ID = "traceId";
private static final String SPAN_ID = "spanId";
private static final String PSPAN_ID = "pspanId";
@Pointcut("execution(public * com.mortals..*Controller.*(..))") @Pointcut("execution(public * com.mortals..*Controller.*(..))")
public void webLog() { public void webLog() {
} }
...@@ -63,18 +68,22 @@ public class WebLogAspect { ...@@ -63,18 +68,22 @@ public class WebLogAspect {
public void afterReturning(Object result) { public void afterReturning(Object result) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest(); HttpServletRequest request = attributes.getRequest();
String traceId = request.getHeader(TRACE_ID);
String pspanId = request.getHeader(PSPAN_ID);
String spanId = request.getHeader(SPAN_ID);
Map<String, String> map = MDC.getCopyOfContextMap(); Map<String, String> map = MDC.getCopyOfContextMap();
if (map != null && result != null) { if (map != null && result != null) {
String startTime = map.getOrDefault("startTime", String.valueOf(System.currentTimeMillis())); String startTime = map.getOrDefault("startTime", String.valueOf(System.currentTimeMillis()));
long takeTime = (System.currentTimeMillis() - Long.parseLong(startTime)); long takeTime = (System.currentTimeMillis() - Long.parseLong(startTime));
if (result instanceof String) { if (result instanceof String) {
log.info(" \n 请求路径:{} 耗时:{}ms \n 请求报文:{} \n 响应报文:{}" log.info(" \n 请求路径:{} 耗时:{}ms \n traceId:{} pspanId:{} spanId:{} \n 请求报文:{} \n 响应报文:{}"
, request.getRequestURI(), takeTime, map.getOrDefault("req", ""), result); , request.getRequestURI(), takeTime,traceId,pspanId,spanId, map.getOrDefault("req", ""), result);
} else { } else {
log.info(" \n 请求路径:{} 耗时:{}ms \n 请求报文:{} \n 响应报文:{}" log.info(" \n 请求路径:{} 耗时:{}ms \n traceId:{} pspanId:{} spanId:{} \n 请求报文:{} \n 响应报文:{}"
, request.getRequestURI(), takeTime, map.getOrDefault("req", ""), JSON.toJSONString(result)); , request.getRequestURI(), takeTime,traceId,pspanId,spanId, map.getOrDefault("req", ""), JSON.toJSONString(result));
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment