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;
    }
}
复制代码

 

posted @   glpa  阅读(144)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示