springAOP拦截并打印controller层请求日志

/**
 * Aop implementation of request RequestControllerLog printing
 *
 * 
 */
@Component
@Aspect
public class RequestControllerLog {

    public static final Logger logger = LoggerFactory.getLogger(RequestControllerLog .class);

    /**
     * Define a pointcut
     */
//    路径 com.xjl.*
    @Pointcut("@annotation(com.xjl.controllerLog)")
    public void controllerLog() {}

    /**
     * Print Log before controller
     * @param joinPoint
     */
    @Before("controllerLog()")
    public void before(JoinPoint joinPoint) throws Exception {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        logger.info("请求IP:{}", request.getRemoteAddr());
        logger.info("请求路径:{}", request.getRequestURL());
        logger.info("请求方式:{}", request.getMethod());
        logger.info("方法描述:{}", getMethodDescription(joinPoint));
        logger.info("请求参数:{}", JSONObject.toJSONString(request.getParameterMap()));

    }

    /**
     * Print the time that request method execution spend
     * @param joinPoint
     * @throws Throwable
     */
    @Around("controllerLog()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object[] args = joinPoint.getArgs();
        Object retVal = joinPoint.proceed(args);
        long endTime = System.currentTimeMillis();
        logger .info("执行时间:{} ms", endTime - startTime);
        logger .info("返回值:{}\n\t", JsonUtils.obj2Json(retVal));
        return retVal;
    }

    /**
     * Print exception
     * @param ex
     */
    @AfterThrowing(throwing = "ex", pointcut = "controllerLog()")
    public void afterThrowing(Throwable ex) {
        logger .error("发生异常:{}", ex.toString());
    }

    /**
     * Acquire the description for annotation target method
     * @param joinPoint
     * @return
     * @throws Exception
     */
    protected String getMethodDescription(JoinPoint joinPoint) throws Exception {
        String targetName = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] arguments = joinPoint.getArgs();
        Class<?> targetClass = Class.forName(targetName);
        Method[] methods = targetClass.getMethods();

        String description = "";
        for (Method method : methods) {
            if(method.getName().equals(methodName)) {
                Class<?>[] clazzs = method.getParameterTypes();
                if(clazzs.length == arguments.length) {
                    description = method.getAnnotation(SystemLog.class).description();
                    break;
                }
            }
        }
        return description;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值