1
0

LoggingAspect.java 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package com.simuwang.deploy.components;
  2. import cn.hutool.core.exceptions.ExceptionUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import cn.hutool.core.util.URLUtil;
  5. import cn.hutool.json.JSONUtil;
  6. import com.simuwang.base.common.util.ServletUtils;
  7. import com.simuwang.logging.Logging;
  8. import com.simuwang.logging.LoggingService;
  9. import com.simuwang.logging.SystemLog;
  10. import jakarta.servlet.http.HttpServletRequest;
  11. import org.aspectj.lang.ProceedingJoinPoint;
  12. import org.aspectj.lang.annotation.Around;
  13. import org.aspectj.lang.annotation.Aspect;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import org.springframework.aop.support.AopUtils;
  17. import org.springframework.core.annotation.Order;
  18. import org.springframework.util.StopWatch;
  19. import java.util.Objects;
  20. /**
  21. * @author wangzaijun
  22. * @date 2024/9/14 14:07
  23. * @description 系统日志拦截切面
  24. */
  25. @Order(2)
  26. @Aspect
  27. public class LoggingAspect {
  28. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  29. private final LoggingService service;
  30. public LoggingAspect(LoggingService service) {
  31. this.service = service;
  32. }
  33. @Around("@annotation(annotation)")
  34. public Object around(ProceedingJoinPoint joinPoint, SystemLog annotation) throws Throwable {
  35. Class<?> clazz = AopUtils.getTargetClass(joinPoint.getTarget());
  36. SystemLog systemLog = clazz.getAnnotation(SystemLog.class);
  37. String title = annotation.value();
  38. if (systemLog != null && StrUtil.isNotBlank(systemLog.value())) {
  39. title = systemLog.value() + "-" + title;
  40. }
  41. HttpServletRequest request = ServletUtils.getRequest();
  42. Logging.Builder builder = Logging.builder().title(title).type(annotation.type().getValue()).method(request.getMethod())
  43. .requestUri(URLUtil.getPath(request.getRequestURI())).remoteAddr(ServletUtils.getIpAddr(request))
  44. .params(joinPoint.getArgs() == null ? Objects.toString(request.getParameterMap()) : JSONUtil.toJsonStr(joinPoint.getArgs()));
  45. StopWatch watch = new StopWatch();
  46. watch.start();
  47. Object result;
  48. try {
  49. result = joinPoint.proceed();
  50. if (annotation.saveResult()) {
  51. builder.result(JSONUtil.toJsonStr(result));
  52. }
  53. } catch (Throwable throwable) {
  54. builder.exception(ExceptionUtil.stacktraceToString(throwable));
  55. builder.hasException(true);
  56. throw throwable;
  57. } finally {
  58. watch.stop();
  59. builder.executeTime(watch.getTotalTimeMillis());
  60. Logging logging = builder.build();
  61. try {
  62. this.service.asyncSave(logging);
  63. } catch (Exception e) {
  64. this.logger.warn("请求{} 日志保存报错\n{}", logging, ExceptionUtil.stacktraceToString(e));
  65. }
  66. }
  67. return result;
  68. }
  69. }