• Springboot整合AOP实现日志的保存


    1.定义注解

    1. /**
    2. * 系统日志元注解
    3. */
    4. @Target(ElementType.METHOD)
    5. @Retention(RetentionPolicy.RUNTIME)
    6. @Documented
    7. public @interface LogFilter {
    8. String value() default "" ;
    9. }

    2.编写切面的实现

    1. @Aspect
    2. @Component
    3. public class LogAspect {
    4. private static final Logger LOGGER = LoggerFactory.getLogger(LogAspect.class) ;
    5. //切入点,已经被增强的连接点。
    6. @Pointcut("@annotation(com.boot.aop.config.LogFilter)")
    7. public void logPointCut (){
    8. }
    9. //通知增强代码
    10. @Around("logPointCut()")
    11. public Object around (ProceedingJoinPoint point) throws Throwable {
    12. Object result = null ;
    13. try{
    14. // 执行方法
    15. result = point.proceed();
    16. // 保存请求日志
    17. saveRequestLog(point);
    18. } catch (Exception e){
    19. // 保存异常日志
    20. saveExceptionLog(point,e.getMessage());
    21. }
    22. return result;
    23. }
    24. /**
    25. * 捕获异常:/ by zero
    26. * 请求路径:http://localhost:8011/saveExceptionLog
    27. * 请求方法:saveExceptionLog
    28. * 模块描述:保存异常日志
    29. * 请求参数:["cicada"]
    30. */
    31. private void saveExceptionLog (ProceedingJoinPoint point,String exeMsg){
    32. LOGGER.info("捕获异常:"+exeMsg);
    33. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    34. LOGGER.info("请求路径:"+request.getRequestURL());
    35. MethodSignature signature = (MethodSignature) point.getSignature();
    36. Method method = signature.getMethod();
    37. LOGGER.info("请求方法:"+method.getName());
    38. // 获取方法上LogFilter注解
    39. LogFilter logFilter = method.getAnnotation(LogFilter.class);
    40. String value = logFilter.value() ;
    41. LOGGER.info("模块描述:"+value);
    42. Object[] args = point.getArgs();
    43. LOGGER.info("请求参数:"+ JSONObject.toJSONString(args));
    44. }
    45. /**
    46. * 请求路径:http://localhost:8011/saveRequestLog
    47. * 请求方法:saveRequestLog
    48. * 模块描述:保存请求日志
    49. * 请求参数:["cicada"]
    50. */
    51. private void saveRequestLog (ProceedingJoinPoint point){
    52. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    53. LOGGER.info("请求路径:"+request.getRequestURL());
    54. MethodSignature signature = (MethodSignature) point.getSignature();
    55. Method method = signature.getMethod();
    56. LOGGER.info("请求方法:"+method.getName());
    57. // 获取方法上LogFilter注解
    58. LogFilter logFilter = method.getAnnotation(LogFilter.class);
    59. String value = logFilter.value() ;
    60. LOGGER.info("模块描述:"+value);
    61. Object[] args = point.getArgs();
    62. LOGGER.info("请求参数:"+ JSONObject.toJSONString(args));
    63. }
    64. }

    3.测试

    1. @LogFilter("保存请求日志")
    2. @RequestMapping("/saveRequestLog")
    3. public String saveRequestLog (@RequestParam("name") String name){
    4. return "success:"+name ;
    5. }
    6. @LogFilter("保存异常日志")
    7. @RequestMapping("/saveExceptionLog")
    8. public String saveExceptionLog (@RequestParam("name") String name){
    9. int error = 100 / 0 ;
    10. System.out.println(error);
    11. return "success:"+name ;
    12. }
    13. }

  • 相关阅读:
    多线程的上下文切换以及线程调度问题和算法
    MySQL之中间件Mycat实现读写分离
    【jetpack】Navigation
    【ML特征工程】第 3 章 :文本数据:扁平化、过滤和分块
    数据结构-排序
    你能分清Java的关键字和标识符吗?
    openfeign客户端A调用服务B,服务B抛出异常时,客户端A接收的几种情况
    Python中的yield简介及用法
    Vue ElementUi 校验邮箱手机号--大全
    Xilinx 高速AD 设计参考(在网上找到的总结)
  • 原文地址:https://blog.csdn.net/qq_44981598/article/details/132758750