编写日志统一处理类

╰半夏微凉° 2021-07-16 22:17 445阅读 0赞
  1. package com.zpc.tet.controller.log;
  2. import org.aspectj.lang.JoinPoint;
  3. import org.aspectj.lang.ProceedingJoinPoint;
  4. import org.aspectj.lang.annotation.AfterReturning;
  5. import org.aspectj.lang.annotation.Around;
  6. import org.aspectj.lang.annotation.Aspect;
  7. import org.aspectj.lang.annotation.Pointcut;
  8. import org.aspectj.lang.reflect.MethodSignature;
  9. import org.springframework.stereotype.Component;
  10. import java.lang.reflect.Method;
  11. /**
  12. * @Author timor
  13. * @Date 2019/8/23
  14. *
  15. * 统一日志格式
  16. */
  17. @Aspect
  18. @Component
  19. public class SysLogAspect {
  20. @Pointcut("@annotation(com.zpc.tet.controller.log.Log)")
  21. public void logPointCut() {
  22. }
  23. @Around("logPointCut()")
  24. public Object around(ProceedingJoinPoint point) throws Throwable {
  25. long beginTime = System.currentTimeMillis();
  26. //执行方法
  27. Object result = point.proceed();
  28. //执行时长(毫秒)
  29. long time = System.currentTimeMillis() - beginTime;
  30. //保存日志
  31. saveSysLog(point, time);
  32. return result;
  33. }
  34. @AfterReturning("logPointCut()")
  35. public void after(JoinPoint point) throws Throwable {
  36. MethodSignature signature = (MethodSignature) point.getSignature();
  37. Method method = signature.getMethod();
  38. System.out.println(method.getAnnotation(Log. class));
  39. String className = point.getTarget().getClass().getName();
  40. String methodName = signature.getName();
  41. System.out.println(className + "." + methodName + "()");
  42. }
  43. private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {
  44. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  45. Method method = signature.getMethod();
  46. System.out.println(method.getAnnotation(Log.class));
  47. //请求的方法名
  48. String className = joinPoint.getTarget().getClass().getName();
  49. String methodName = signature.getName();
  50. System.out.println(className + "." + methodName + "()");
  51. //请求的参数
  52. Object[] args = joinPoint.getArgs();
  53. System.out.println(args[0]);
  54. }
  55. }

发表评论

表情:
评论列表 (有 0 条评论,445人围观)

还没有评论,来说两句吧...

相关阅读

    相关 统一日志处理

    一、日志 1、什么是日志 大家在学习Java开发的过程中,都要接触日志功能,不管是在打印控制台中还是记录到文件,我们都需要使用日志功能,通过日志查看程序运行过程,运