LogAspect.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package com.ydtech.aop;
  2. import cn.dev33.satoken.stp.StpUtil;
  3. import com.alibaba.fastjson.JSON;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.ydtech.core.page.HttpResult;
  6. import com.ydtech.modules.admin.model.SysLog;
  7. import com.ydtech.modules.admin.service.SysLogService;
  8. import io.swagger.annotations.ApiOperation;
  9. import org.aspectj.lang.JoinPoint;
  10. import org.aspectj.lang.ProceedingJoinPoint;
  11. import org.aspectj.lang.annotation.*;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Component;
  16. import org.springframework.web.context.request.RequestContextHolder;
  17. import org.springframework.web.context.request.ServletRequestAttributes;
  18. import javax.servlet.http.HttpServletRequest;
  19. import java.lang.reflect.Method;
  20. import java.util.Date;
  21. /**
  22. * 日志切面类
  23. * AOP切面,记录方法的调用,入参以及出參
  24. */
  25. @Aspect
  26. @Component
  27. public class LogAspect {
  28. private final static Logger LOGGER = LoggerFactory.getLogger(LogAspect.class);
  29. @Autowired
  30. private GlobalExceptionHandler exceptionHandle;
  31. @Autowired
  32. private SysLogService sysLogService;
  33. @Pointcut("execution(public * com.ydtech.modules.*.controller.*.*(..)) ")
  34. public void log() {
  35. }
  36. @Before("log()")
  37. public void doBefore(JoinPoint joinPoint) {
  38. ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
  39. HttpServletRequest request = attributes.getRequest();
  40. String userId = StpUtil.getLoginId("").toString();
  41. //请求人 url method ip class_method
  42. SysLog sysLog = new SysLog(userId, request.getRequestURL().toString(), request.getMethod(),
  43. joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(), request.getRemoteAddr(), new Date());
  44. try {
  45. //获取注解注释
  46. String annotationValue = getAnnotationValue(joinPoint);
  47. sysLog.setDescription(annotationValue);
  48. //获取请求参数
  49. Object[] args = joinPoint.getArgs();
  50. for (Object obj : args) {
  51. if (null != obj && (obj.getClass().getName().startsWith("com.ydtech.modules") || obj instanceof String)) {
  52. sysLog.setParams(JSON.toJSON(obj).toString());
  53. }
  54. }
  55. if (request.getMethod().equals("GET")) {
  56. sysLog.setParams(request.getQueryString());
  57. }
  58. sysLog.preInsert();
  59. sysLogService.insert(sysLog);
  60. } catch (ClassNotFoundException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. @Around("log()")
  65. public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
  66. HttpResult result = null;
  67. try {
  68. } catch (Exception e) {
  69. return exceptionHandle.exceptionGet(e);
  70. }
  71. if (result == null) {
  72. return proceedingJoinPoint.proceed();
  73. } else {
  74. return result;
  75. }
  76. }
  77. @AfterReturning(pointcut = "log()", returning = "object")//打印输出结果
  78. public void doAfterReturing(Object object) {
  79. if (object != null) {
  80. LOGGER.info("response={}", object.toString());
  81. }
  82. }
  83. private String getAnnotationValue(JoinPoint joinPoint) throws ClassNotFoundException {
  84. //反射获取类
  85. Class<?> clazz = Class.forName(joinPoint.getSignature().getDeclaringTypeName());
  86. //通过方法名反射获取method
  87. Method[] declaredMethods = clazz.getDeclaredMethods();
  88. //请求方法名
  89. String requestName = joinPoint.getSignature().getName();
  90. for (Method method : declaredMethods) {
  91. String name = method.getName();
  92. //获取方法上的注解
  93. if (name.equals(requestName)) {
  94. ApiOperation annotation = method.getAnnotation(ApiOperation.class);
  95. if (annotation != null) {
  96. return annotation.value();
  97. }
  98. }
  99. }
  100. return null;
  101. }
  102. }