前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于aop的注解对方法调用前后打日志

基于aop的注解对方法调用前后打日志

作者头像
saintyyu
发布2021-11-22 09:43:08
2710
发布2021-11-22 09:43:08
举报
文章被收录于专栏:IT专栏

注解:

代码语言:javascript
复制
@Target({METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodLog {
    /**
     * 请求是否打日志
     * @return
     */
    boolean reqLog() default true;

    /**
     * 返回是否打日志
     * @return
     */
    boolean respLog() default true;

}

aop:

代码语言:javascript
复制
@Aspect
@Component
@Slf4j
public class MethodLogAop {

    @Around("@annotation(MethodLog)")
    public Object methodLog(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("before invoke: " + System.currentTimeMillis() + ","
                + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName() + ", requests : "
                + JSON.toJSONString(Arrays.asList(joinPoint.getArgs()),
                SerializerFeature.DisableCircularReferenceDetect));
        Object obj;
        try {
            obj = joinPoint.proceed();
        } catch (Throwable e) {
            log.error("exception invoke: "  + System.currentTimeMillis() + ","
                    + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName() + "exception: "
                    + e.getMessage());
            throw e;
        }
        log.info("after invoke: "  + System.currentTimeMillis() + ","
                + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName() + ", response : "
                + JSON.toJSONString(obj, SerializerFeature.DisableCircularReferenceDetect));
        return obj;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/08/18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档