• 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. }

  • 相关阅读:
    springcloud集成Seata AT 模式
    windows系统下doccano标注工具使用
    玩家最关心的绝地求生游戏作战干货大揭秘,助你击败敌人登顶王者!
    springboot 配置 servlet filter 2
    动手学深度学习(五)Kaggle房价预测
    模型资源加载引起的内存对齐问题
    【Linux】《Linux命令行与shell脚本编程大全 (第4版) 》笔记-Chapter8-管理文件系统
    基于STM32设计的智慧路灯(太阳能+华为云IOT)
    【云原生】学习K8s,读完这篇就够了
    全网没有之一的API 文档:Swagger
  • 原文地址:https://blog.csdn.net/qq_44981598/article/details/132758750