zoukankan      html  css  js  c++  java
  • AOP日志切面

    @Component
    @Aspect
    public class RequestLogAspect {
    
        private final static Logger LOGGER = LoggerFactory.getLogger(RequestLogAspect.class);
    
        @Pointcut("execution(* com.doke.controller.*.*(..))")
        public void requestServer() {
        }
    
        @Around("requestServer()")
        public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
            long start = System.currentTimeMillis();
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            Object result = proceedingJoinPoint.proceed();
            RequestInfo requestInfo = new RequestInfo(
                    request.getRemoteAddr(),
                    request.getRequestURL().toString(),
                    request.getMethod(),
                    String.format("%s.%s", proceedingJoinPoint.getSignature().getDeclaringTypeName(),
                            proceedingJoinPoint.getSignature().getName()),
                    getRequestParamsByProceedingJoinPoint(proceedingJoinPoint),
                    result,
                    System.currentTimeMillis() - start
            );
            LOGGER.info("Request Info      : {}", JSON.toJSONString(requestInfo));
            return result;
        }
    
        @AfterThrowing(pointcut = "requestServer()", throwing = "e")
        public void doAfterThrow(JoinPoint joinPoint, RuntimeException e) {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            RequestErrorInfo requestErrorInfo = new RequestErrorInfo(
                    request.getRemoteAddr(),
                    request.getRequestURL().toString(),
                    request.getMethod(),
                    String.format("%s.%s", joinPoint.getSignature().getDeclaringTypeName(),
                            joinPoint.getSignature().getName()),
                    getRequestParamsByJoinPoint(joinPoint),
                    e
            );
            LOGGER.info("Error Request Info      : {}", JSON.toJSONString(requestErrorInfo));
        }
    
        /**
         * 获取入参
         * @param proceedingJoinPoint
         *
         * @return
         * */
        private Map<String, Object> getRequestParamsByProceedingJoinPoint(ProceedingJoinPoint proceedingJoinPoint) {
            //参数名
            String[] paramNames = ((MethodSignature)proceedingJoinPoint.getSignature()).getParameterNames();
            //参数值
            Object[] paramValues = proceedingJoinPoint.getArgs();
    
            return buildRequestParam(paramNames, paramValues);
        }
    
        private Map<String, Object> getRequestParamsByJoinPoint(JoinPoint joinPoint) {
            //参数名
            String[] paramNames = ((MethodSignature)joinPoint.getSignature()).getParameterNames();
            //参数值
            Object[] paramValues = joinPoint.getArgs();
    
            return buildRequestParam(paramNames, paramValues);
        }
    
        private Map<String, Object> buildRequestParam(String[] paramNames, Object[] paramValues) {
            Map<String, Object> requestParams = new HashMap<>();
            for (int i = 0; i < paramNames.length; i++) {
                Object value = paramValues[i];
    
                //如果是文件对象
                if (value instanceof MultipartFile) {
                    MultipartFile file = (MultipartFile) value;
                    value = file.getOriginalFilename();  //获取文件名
                }
    
                requestParams.put(paramNames[i], value);
            }
    
            return requestParams;
        }
    
        @Data
        @AllArgsConstructor
        public class RequestInfo {
            private String ip;
            private String url;
            private String httpMethod;
            private String classMethod;
            private Object requestParams;
            private Object result;
            private Long timeCost;
        }
    
        @Data
        @AllArgsConstructor
        public class RequestErrorInfo {
            private String ip;
            private String url;
            private String httpMethod;
            private String classMethod;
            private Object requestParams;
            private RuntimeException exception;
        }
    View Code
  • 相关阅读:
    【bzoj4276】[ONTAK2015]Bajtman i Okrągły Robin 线段树优化建图+费用流
    【bzoj4383】[POI2015]Pustynia 线段树优化建图+差分约束系统+拓扑排序
    【bzoj4519】[Cqoi2016]不同的最小割 分治+最小割
    【bzoj2229】[Zjoi2011]最小割 分治+网络流最小割
    【bzoj3689】异或之 可持久化Trie树+堆
    【bzoj1109】[POI2007]堆积木Klo 动态规划+树状数组
    【bzoj2780】[Spoj]8093 Sevenk Love Oimaster 广义后缀自动机
    【bzoj4804】欧拉心算 欧拉函数
    【bzoj3231】[Sdoi2008]递归数列 矩阵乘法+快速幂
    【bzoj3589】动态树 树链剖分+线段树
  • 原文地址:https://www.cnblogs.com/vip-nange/p/14001734.html
Copyright © 2011-2022 走看看