springboot 统一日志记录 - AOP日志
参考学习:
https://www.bilibili.com/video/BV1bf4y187KX/
三步:1.使用日志注解。2.编写日志注解类。3.编写日志注解类的AOP实现。
1.在需要记日志处,使用自定义的注解。
package com.springboot.controller; import com.springboot.annotation.LogAnnotation; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/hello") public class HelloController { @LogAnnotation("日志内容") @GetMapping("/{name}") public String sayHello(@PathVariable String name) { return "hello " + name; }
2.编写日志注解类
@Target({ ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) public @interface LogAnnotation { String value() default ""; }
3.编写日志注解类的AOP实现
@Aspect @Component public class LogAdvice { @Around(value = "@annotation(com.springboot.annotation.LogAnnotation)") public Object savaLog(ProceedingJoinPoint joinPoint) throws Throwable { MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); LogAnnotation logAnnotation = methodSignature.getMethod().getDeclaredAnnotation(LogAnnotation.class) // do something save 日志 //执行方法 Object object = joinPoint.proceed(); return object; } }