• Spring AOP拦截并打印controller层请求日志


    /**
     * Aop implementation of request log printing
     *
     * Created b on 9:07 2017/10/27
     */
    @Component
    @Aspect
    public class RequestLog {
    
        public static final Logger LOG = LoggerFactory.getLogger(RequestLog.class);
    
        /**
         * Define a pointcut
         */
    //    @Pointcut("execution(* com.wqxia.*.*(..))")
        @Pointcut("@annotation(com.wqxia.common.log.annotation.SystemLog)")
        public void controllerLog() {}
    
        /**
         * Print Log before controller
         * @param joinPoint
         */
        @Before("controllerLog()")
        public void before(JoinPoint joinPoint) throws Exception {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    
            LOG.info("请求IP:{}", request.getRemoteAddr());
            LOG.info("请求路径:{}", request.getRequestURL());
            LOG.info("请求方式:{}", request.getMethod());
            LOG.info("方法描述:{}", getMethodDescription(joinPoint));
            LOG.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();
            LOG.info("执行时间:{} ms", endTime - startTime);
            LOG.info("返回值:{}
    	", JsonUtils.obj2Json(retVal));
            return retVal;
        }
    
        /**
         * Print exception
         * @param ex
         */
        @AfterThrowing(throwing = "ex", pointcut = "controllerLog()")
        public void afterThrowing(Throwable ex) {
            LOG.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;
        }
    }
    注解:
    /**
     * System log annotation for controller or service
     * Created on 9:10 2017/11/2
     */
    @Target({ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface SystemLog {
    
        String description() default "";
    }
    注解加在controller层方法上即可
  • 相关阅读:
    五分钟完成 ABP vNext 通讯录 App 开发
    .NET Conf: Xamarin专场会议3.23 开幕
    2020 年 中国.NET开发者调查报告
    推荐一个很棒的开源工作流elsa-core
    尝试使用 Visual Studio Online (Cloud IDE)
    Mongo2Go 介绍
    DevExpress作为企业赞助商加入.NET基金会
    【新书推荐】《ASP.NET Core微服务实战:在云环境中开发、测试和部署跨平台服务》 带你走近微服务开发
    云原生时代 来看看十年前李彦宏、马化腾和马云对云计算的评价
    .NET 在云原生时代的蜕变,让我在云时代脱颖而出
  • 原文地址:https://www.cnblogs.com/junzi2099/p/14208667.html
Copyright © 2020-2023  润新知