首页 > 其他分享 >springboot 统一日志记录 - AOP日志

springboot 统一日志记录 - AOP日志

时间:2023-01-31 10:38:56浏览次数:66  
标签:springboot LogAnnotation AOP 注解 日志 public

参考学习:

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;
    }
}

 

标签:springboot,LogAnnotation,AOP,注解,日志,public
From: https://www.cnblogs.com/ncepu/p/17078128.html

相关文章