LogAspect.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package com.imed.costaccount.common.aop;
  2. import cn.hutool.core.net.NetUtil;
  3. import cn.hutool.json.JSONUtil;
  4. import com.imed.costaccount.common.util.Result;
  5. import io.swagger.annotations.ApiOperation;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.aspectj.lang.JoinPoint;
  8. import org.aspectj.lang.Signature;
  9. import org.aspectj.lang.annotation.AfterReturning;
  10. import org.aspectj.lang.annotation.Aspect;
  11. import org.aspectj.lang.annotation.Before;
  12. import org.aspectj.lang.annotation.Pointcut;
  13. import org.aspectj.lang.reflect.MethodSignature;
  14. import org.springframework.stereotype.Component;
  15. import org.springframework.web.context.request.RequestContextHolder;
  16. import org.springframework.web.context.request.ServletRequestAttributes;
  17. import javax.servlet.http.HttpServletRequest;
  18. @Aspect
  19. @Slf4j
  20. @Component
  21. public class LogAspect {
  22. /**
  23. * ..表示包及子包 该方法代表controller层的所有方法 TODO 路径需要根据自己项目定义
  24. */
  25. @Pointcut("execution(public * com.imed.costaccount.web.*.*(..))")
  26. public void controllerMethod() {
  27. }
  28. /**
  29. * 方法执行前
  30. *
  31. * @param joinPoint
  32. * @throws Exception
  33. */
  34. @Before("controllerMethod()")
  35. public void LogRequestInfo(JoinPoint joinPoint) throws Exception {
  36. ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
  37. HttpServletRequest request = attributes.getRequest();
  38. StringBuilder requestLog = new StringBuilder();
  39. Signature signature = joinPoint.getSignature();
  40. requestLog.append(((MethodSignature) signature).getMethod().getAnnotation(ApiOperation.class).value()).append("\t")
  41. .append("请求信息:").append("URL = {").append(request.getRequestURI()).append("},\t")
  42. .append("请求方式 = {").append(request.getMethod()).append("},\t")
  43. .append("请求IP = {").append(NetUtil.getLocalhostStr()).append("},\t")
  44. .append("类方法 = {").append(signature.getDeclaringTypeName()).append(".")
  45. .append(signature.getName()).append("},\t");
  46. // 处理请求参数
  47. String[] paramNames = ((MethodSignature) signature).getParameterNames();
  48. Object[] paramValues = joinPoint.getArgs();
  49. int paramLength = null == paramNames ? 0 : paramNames.length;
  50. if (paramLength == 0) {
  51. requestLog.append("请求参数 = {} ");
  52. } else {
  53. requestLog.append("请求参数 = [");
  54. for (int i = 0; i < paramLength - 1; i++) {
  55. requestLog.append(paramNames[i]).append("=").append(JSONUtil.toJsonStr(paramValues[i])).append(",");
  56. }
  57. requestLog.append(paramNames[paramLength - 1]).append("=").append(JSONUtil.toJsonStr(paramValues[paramLength - 1])).append("]");
  58. }
  59. log.info(requestLog.toString());
  60. }
  61. /**
  62. * 方法执行后
  63. *
  64. * @param result
  65. * @throws Exception
  66. */
  67. @AfterReturning(returning = "result", pointcut = "controllerMethod()")
  68. public void logResultVOInfo(Result result) throws Exception {
  69. log.info("请求结果:" + result.getStatus() + "\t" + result.getMsg());
  70. }
  71. }