LogControllerAspect.java 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package com.mokasz.sample.common.aspect;
  2. import com.alibaba.fastjson.JSON;
  3. import com.mokasz.sample.common.context.ApiContext;
  4. import com.plumelog.core.TraceId;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.aspectj.lang.ProceedingJoinPoint;
  7. import org.aspectj.lang.annotation.AfterReturning;
  8. import org.aspectj.lang.annotation.Around;
  9. import org.aspectj.lang.annotation.Aspect;
  10. import org.aspectj.lang.annotation.Pointcut;
  11. import org.aspectj.lang.reflect.MethodSignature;
  12. import org.springframework.core.annotation.Order;
  13. import org.springframework.stereotype.Component;
  14. import org.springframework.web.context.request.RequestContextHolder;
  15. import org.springframework.web.context.request.ServletRequestAttributes;
  16. import javax.servlet.http.HttpServletRequest;
  17. import java.time.Duration;
  18. import java.time.Instant;
  19. import java.util.Date;
  20. import java.util.Enumeration;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. @Aspect
  24. @Component
  25. @Order(2)
  26. @Slf4j
  27. public class LogControllerAspect {
  28. @Pointcut("execution(* com.mokasz.sample.*.controller.*.*(..))")
  29. private void pointcut() {
  30. }
  31. @Around("pointcut()")
  32. public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
  33. Instant begin = new Date().toInstant();
  34. ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
  35. HttpServletRequest request = attributes.getRequest();
  36. MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
  37. TraceId.logTraceID.set(ApiContext.getTraceId());
  38. log.info("[LogControllerAspect]调用开始 params:{},args:{},requestUrl:{}"
  39. , methodSignature.getParameterNames()
  40. , joinPoint.getArgs()
  41. , JSON.toJSONString(request.getRequestURL()));
  42. Object proceed = joinPoint.proceed();
  43. String result = JSON.toJSONString(proceed);
  44. Instant end = new Date().toInstant();
  45. log.info("[LogControllerAspect]调用结束 params:{},args:{},requestUrl:{},response:{},cost:{},speed:{},headerMap:{}"
  46. , methodSignature.getParameterNames()
  47. , joinPoint.getArgs()
  48. , JSON.toJSONString(request.getRequestURL())
  49. , JSON.toJSONString(result)
  50. , Duration.between(begin, end).toMillis()
  51. , Duration.between(begin, end).toMillis() > 500 ? "<<slow>>" : "<<fast>>"
  52. , getHeaderMap(request));
  53. return proceed;
  54. }
  55. @AfterReturning("pointcut()")
  56. public void afterReturning() {
  57. ApiContext.removeApiContextModel();
  58. }
  59. private Map<String, String> getHeaderMap(HttpServletRequest request) {
  60. Enumeration<String> headerNames = request.getHeaderNames();
  61. Map<String, String> headerMap = new HashMap<>(16);
  62. while (headerNames.hasMoreElements()) {
  63. String headName = headerNames.nextElement();
  64. String headValue = request.getHeader(headName);
  65. headerMap.put(headName, headValue);
  66. }
  67. return headerMap;
  68. }
  69. }