自定义AOP监控controller层的方法执行信息打印日志

自定义AOP监控controller层的方法执行

WebLog

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
public @interface WebLog {
    String value() default "";

    String module() default "";

    String desc() default "";

    String type() default "";
}

AopConfig

@Slf4j
@Aspect
@Component
public class AopConfig {

@Pointcut("@annotation(com.dem.annotation.WebLog)")
public void webLog(){

}
@Around("webLog()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
    //拿取WebLog里面的值(module:模块,desc:描述)
    MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
    Method method = methodSignature.getMethod();
    WebLog webLogger = method.getAnnotation(WebLog.class);
    String module = webLogger.module();
    String desc = webLogger.desc();

    long startTime = System.currentTimeMillis();
    Object result = null;
    try {
        result = joinPoint.proceed();
        log.info("Response:" + JacksonUtils.beanToJson(result));
        log.info("耗时:" + (System.currentTimeMillis() - startTime) + "ms");
        if (StringUtils.checkIsStrNull(StringUtils.obj2str(result))){
            throw new CommonException(ErrorCode.LOG_CODE_0002);
        }
        JSONObject obj = (JSONObject) JSONObject.toJSON(result);
        String status = obj.getString("code");
        String msg = obj.getString("error");

    } catch (Throwable e) {
        JSONObject obj = (JSONObject) JSONObject.toJSON(e);
        String status = obj.getString("code");
        String msg = obj.getString("msg");

        log.error("错误码:"+status);
        log.error("错误日志:"+e.getMessage());
        log.error(e.getMessage(),e);
        throw new CommonException(ErrorCode.FAIL);
    }
    return result;
}

@Before(value="webLog()")
    public void beforeControll(JoinPoint joinPoint) throws Exception{
    ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    HttpServletRequest request = attributes.getRequest();
    log.info("==================Start=================");
    log.info("URL:" + request.getRequestURL().toString());
    log.info("QueryString:" + request.getQueryString());
    log.info("Description:" + getLogValue(joinPoint));
    log.info("Method:" + request.getMethod());
    log.info("Class Method:" + joinPoint.getSignature().getDeclaringTypeName() + "," + joinPoint.getSignature().getName());
    log.info("客户端IP:" + request.getRemoteAddr());
    log.info("请求参数:" + JacksonUtils.beanToJson(joinPoint.getArgs()));
}
    @After(value="webLog()")
    public void afterControll() throws Exception{
    log.info("结束了");
    }
    @AfterReturning("webLog()")
    public void doAfter() throws Exception {
        log.info("==================End=================");
    }

    private String getLogValue(JoinPoint joinPoint) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        WebLog webLogger = method.getAnnotation(WebLog.class);
        return webLogger.value();
    }
}

然后在controller层方法上方加注解
@WebLog(module = “模块名称”,desc =“描述” )

其中用到了几个工具类,需要请在博客工具文章中寻找:
工具类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值