AOP警告日志监控和打印sql语句

打印sql语句
在Application.yml 中添加

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

aop配置
导入依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

@Around(value = "execution(* com.lyra.*.service..*.*(..))") 环绕消息具体看切面表达式
Object result = proceedingJoinPoint.proceed(); 开始执行代理方法
语句执行时间超过3000发出error 200发出警告

@Aspect
@Component
public class ServiceLogAspect {
    private final Logger logger = LoggerFactory.getLogger(ServiceLogAspect.class);

    @Around(value = "execution(* com.lyra.*.service..*.*(..))")
    public Object recordTimeOfService(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        // target 为切入点所在目标对象
        logger.info("====开始执行 {}, {}", proceedingJoinPoint.getTarget().getClass(),
                proceedingJoinPoint.getSignature().getName());



        Signature signature = proceedingJoinPoint.getSignature();

        // 获取调用方法返回值 包名类名 方法名以及参数
        logger.info(signature.toString());

        // 返回方法名称
        System.out.println(signature.getName());
        // 返回类的全限定名
        System.out.println(signature.getDeclaringType());
        // 返回包名
        System.out.println(signature.getDeclaringTypeName());
        // 返回修饰 如public private final ...   
        System.out.println((Modifier.toString(signature.getModifiers())));

        long beginCurrenTime = System.currentTimeMillis();

        // 调用方法链
        Object result = proceedingJoinPoint.proceed();
        long overCurrentTime = System.currentTimeMillis();

        long takeTime = overCurrentTime - beginCurrenTime + 3000;


        if (takeTime > 3000) {
            logger.error("当前执行耗时:{}", takeTime);
        } else if (takeTime > 2000) {
            logger.warn("当前执行耗时:{}", takeTime);
        } else {
            logger.info("当前执行耗时:{}", takeTime);
        }
        return result;
    }
}
posted @   RainbowMagic  阅读(314)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示