【Spring】SpringAop给所有Service增加日志

java 复制代码
@Component
@Aspect
@Slf4j
public class ServiceLogAop {

    @Pointcut("within(@org.springframework.stereotype.Service *)")
    public void serviceMethods() {
    }

    @Before(value = "serviceMethods()")
    public void logMethodEntry(JoinPoint joinPoint) {
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();

        if (args.length != 0) {
            try {
                log.info("========ServiceLog==Param======: " + className + "." + methodName + " with arguments: " + JSON.toJSONString(args));
            } catch (Exception e) {
                log.info("========ServiceLog==Param======: " + className + "." + methodName + " with arguments: " + Arrays.toString(args));
            }
        }
    }

    @AfterReturning(value = "serviceMethods()", returning = "result")
    public void logMethodExit(JoinPoint joinPoint, Object result) {
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();

        if (args.length != 0) {
            try {
                log.info("========ServiceLog==result======: " + className + "." + methodName + " with result: " + JSON.toJSONString(result));
            } catch (Exception e) {
                log.info("========ServiceLog==result======: error");
            }
        }
    }
}

其他的如controller同理

java 复制代码
@Pointcut("within(@org.springframework.stereotype.Service *) || within(@org.springframework.stereotype.Controller *)")

还可以排除

java 复制代码
@Pointcut("within(@org.springframework.stereotype.Service *) && !execution(* springfox.documentation.schema.property.*.*(..))")
相关推荐
C4程序员5 分钟前
Java百度身份证识别接口实现【配置即用】
java·开发语言
炒空心菜菜18 分钟前
MapReduce 实现 WordCount
java·开发语言·ide·后端·spark·eclipse·mapreduce
zy happy36 分钟前
搭建运行若依微服务版本ruoyi-cloud最新教程
java·spring boot·spring cloud·微服务·ruoyi
芯眼44 分钟前
STM32启动文件详解(重点)
java·开发语言·c++·stm32·单片机·mybatis
想躺平的小农1 小时前
EasyExcel详解
java
慧一居士1 小时前
EasyExcel集成使用总结与完整示例
java·excel
呦呦彬1 小时前
【问题排查】easyexcel日志打印Empty row!
java·开发语言·log4j
九章云极AladdinEdu2 小时前
GPU与NPU异构计算任务划分算法研究:基于强化学习的Transformer负载均衡实践
java·开发语言·人工智能·深度学习·测试工具·负载均衡·transformer
佩奇的技术笔记2 小时前
Java学习手册:客户端负载均衡
java·负载均衡
可乐加.糖2 小时前
项目版本管理和Git分支管理方案
java·git·目标跟踪·gitlab·敏捷流程·源代码管理