方法日志切面

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface TimeConsumeLog {
    boolean skipResponseLog() default false;
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface LogParam {
}
@Slf4j
@Aspect
@Component
public class TimeConsumeLogAspect {
    /**
     * 日志前缀
     */
    static String LOG_PREFIX = "[%s]-[%s]";
    /**
     * 日志后缀
     */
    static String LOG_SUFFIX = "%s-参数[%s]";
    /**
     * 异常后缀
     */
    static String EXCEPTION_SUFFIX = "{}-耗时[{}]毫秒-异常:{}";
    /**
     * 耗时后缀
     */
    static String TIME_SUFFIX = "{}-耗时[{}]毫秒-请求结果:[{}]";

    /**
     * 记录方法执行时间
     *
     * @param proceedingJoinPoint
     * @param logExecuteTime
     * @return
     */
    @Around("@annotation(logExecuteTime)")
    public Object logExecuteTime(ProceedingJoinPoint proceedingJoinPoint, TimeConsumeLog logExecuteTime) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object[] args = proceedingJoinPoint.getArgs();
        Object[] logArgList = new Object[args.length];
        Class<?> targetClass = proceedingJoinPoint.getTarget().getClass();

        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
        Method method = targetClass.getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
        Annotation[][] parameterAnnotations = method.getParameterAnnotations();
        // 用了LogParam注解的实参都打印出来
        for (int argIndex = 0; argIndex < args.length; argIndex++) {
            for (Annotation paramAnnotation : parameterAnnotations[argIndex]) {
                if (!(paramAnnotation instanceof LogParam)) {
                    continue;
                }
                logArgList[argIndex] = args[argIndex];
            }
        }
        String className = targetClass.getName();
        String message = String.format(LOG_PREFIX, className, method.getName());
        StringBuilder argLogStringBD = new StringBuilder();
        for (Object ag : logArgList) {
            if (null != ag) {
                argLogStringBD.append(String.format("%s:%s ", ag.getClass().getSimpleName(), JsonUtil.toJSON(ag)));
            }
        }
        if (StringUtil.isNotEmpty(argLogStringBD.toString())) {
            message = String.format(LOG_SUFFIX, message, argLogStringBD.toString());
        }

        Object result = null;
        Throwable tb = null;
        try {
            result = proceedingJoinPoint.proceed();
        } catch (Throwable throwable) {
            tb = throwable;
            throw throwable;
        } finally {
            long consumeTs = System.currentTimeMillis() - startTime;
            if (null != tb) {
                // 异常时也把参数打出来
                log.error(EXCEPTION_SUFFIX, message, consumeTs, ExceptionUtils.getFullStackTrace(tb));
            } else {
                if (logExecuteTime.skipResponseLog()) {
                    log.info(TIME_SUFFIX, message, consumeTs, "方法返回结果的日志已跳过");
                } else {
                    String jsonString = null != result ? JsonUtil.toJSON(result) : "null";
                    log.info(TIME_SUFFIX, message, consumeTs, jsonString);
                }
            }
        }
        return result;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值