springboot自定义注解实现日志切面(基于类和方法)

@Documented
@Retention(RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface ParamResultLog {

}
@Component
@Aspect
public class ParamResultLogAspect {

    private Logger logger = LoggerFactory.getLogger(getClass());

  
    @Pointcut("@annotation(com.*类全路径*.ParamResultLog) || @within(com.*类全路径*.ParamResultLog)")
    private void cut() { }

    private Map<String,Long> timeMap = new HashMap<>();

    @Before("cut()")
    public void doBefore(JoinPoint joinPoint) {
        //类名
        String className = joinPoint.getSignature().getDeclaringTypeName();
        //方法名称
        String methodName = joinPoint.getSignature().getName();
        //方法开始时间
        timeMap.put(Thread.currentThread().getId()+className+":"+methodName+"start",System.currentTimeMillis());
        //防止参数转换报错
        try{
            //方法参数
            Object[] param = new Object[joinPoint.getArgs().length];
            for (int i = 0;i<joinPoint.getArgs().length;i++){
                if(joinPoint.getArgs()[i] != null&&!(joinPoint.getArgs()[i] instanceof Proxy)){
                    param[i] = joinPoint.getArgs()[i];
                }else{
                    param[i] = null;
                }
            }
            String argsJsonStr = JSON.toJSONString(param);
            logger.info("【{}:{}】开始执行,参数:{}",className,methodName,argsJsonStr);

        }catch (Exception e){
            logger.warn("Before日志参数转换异常,不影响业务逻辑!信息:{}",e.getMessage());
        }
    }

    @AfterReturning(returning = "ret", pointcut = "cut()")
    public void doAfterReturning(JoinPoint joinPoint, Object ret) {
        //类名
        String className = joinPoint.getSignature().getDeclaringTypeName();
        //方法名称
        String methodName = joinPoint.getSignature().getName();
        //防止参数转换报错
        try{
            //方法开始时间
            timeMap.put(Thread.currentThread().getId()+className+":"+methodName+"end",System.currentTimeMillis());
            Long cost = timeMap.get(Thread.currentThread().getId()+className+":"+methodName+"end")-timeMap.get(Thread.currentThread().getId()+className+":"+methodName+"start");
            String retStr = JSON.toJSONString(ret);
            logger.info("【{}:{}】执行完毕,返回值:{};花费时间{}ms",className,methodName,retStr,cost);
        }catch (Exception e){
            logger.warn("AfterReturning日志参数转换异常,不影响业务逻辑!信息:{}",e.getMessage());
        }finally {
            timeMap.remove(Thread.currentThread().getId()+className+":"+methodName+"start");
            timeMap.remove(Thread.currentThread().getId()+className+":"+methodName+"end");
        }

    }

    @AfterThrowing(pointcut = "cut()", throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint, Throwable e) {
        //类名
        String className = joinPoint.getSignature().getDeclaringTypeName();
        //方法名称
        String methodName = joinPoint.getSignature().getName();
        try{
            //方法开始时间
            timeMap.put(Thread.currentThread().getId()+className+":"+methodName+"end",System.currentTimeMillis());
            Long cost = timeMap.get(Thread.currentThread().getId()+className+":"+methodName+"end")-timeMap.get(Thread.currentThread().getId()+className+":"+methodName+"start");
            logger.warn("【{}:{}】执行完毕,发生异常:{};花费时间{}ms",className,methodName,e.getMessage(),cost);
        }catch (Exception e1){
            logger.warn("AfterThrowing日志参数转换异常,不影响业务逻辑!信息:{}",e.getMessage());
        }finally {
            timeMap.remove(Thread.currentThread().getId()+className+":"+methodName+"start");
            timeMap.remove(Thread.currentThread().getId()+className+":"+methodName+"end");
        }
    }

}

@ParamResultLog 可作用于类或者某个方法,实现方法的出入参打印,方法执行时间捕获。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值