zoukankan      html  css  js  c++  java
  • 基础组件-统一服务日志切面

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
            </dependency>
    /**
     * @author: Gabriel
     * @date: 2020/1/28 22:08
     * @description 服务日志切面
     */
    @Slf4j
    @Aspect
    @Component
    public class SysLogAspect {
    
        /** 注解切点 */
        @Pointcut("@annotation(org.springframework.web.bind.annotation.GetMapping)||" +
                  "@annotation(org.springframework.web.bind.annotation.PostMapping)||" +
                  "@annotation(org.springframework.web.bind.annotation.PutMapping)||" +
                  "@annotation(org.springframework.web.bind.annotation.DeleteMapping)")
        public void requestMapping(){
        }
    
        /** 控制器方法切点 */
        @Pointcut("execution(* com.gabriel.stage.controller.*Controller.*(..))")
        public void methodPointCut(){
        }
    
        @Around("requestMapping() && methodPointCut()")
        public Object logAround(ProceedingJoinPoint pjd)throws Throwable{
            Signature signature = pjd.getSignature();
            //请求类名
            String className = signature.getClass().getName();
            //请求方法名
            String methodName = signature.getName();
            //记录日志入库
            Object[] requestParamArray = pjd.getArgs();
            StopWatch watch =new StopWatch();
            watch.start();
            int index =0;
            StringBuffer sb = new StringBuffer();
            for (Object requestParam : requestParamArray) {
                if (ObjectUtil.isNull(requestParam)) {
                    index++;
                    continue;
                }
                try {
                    sb.append(JSON.toJSONString(requestParam));
    
                    //
                }catch (Exception e){
                    sb.append(requestParam.toString());
                }
                sb.append(",");
            }
            String concatParam = sb.toString();
    
            if (concatParam.length()>0) {
                concatParam=concatParam.substring(0,concatParam.length()-1);
            }
    
            //记录请求
            log.info(String.format("【%s】类的【%s】方法,请求参数:%s",className,methodName,concatParam));
    
            //执行服务方法-环绕通知能决定是否执行目标方法,且返回值与目标方法的返回值一致
            Object response = pjd.proceed();
    
    
            watch.stop();
    
            //记录应答
            log.info(String.format("【%s】类的【%s】方法,返回参数:%s",className,methodName,JSON.toJSONString(response)));
    
            // 获取执行完的时间
            log.info(String.format("接口【%s】总耗时(毫秒):%s", methodName, watch.getTotalTimeMillis()));
    
    
            System.out.println("Spring AOP方式记录标准请求-应答模型服务日志");
            //请求参数
            Object request=null;
    
            if (ArrayUtil.isNotEmpty(requestParamArray)) {
                 request = requestParamArray[index];
            }
    
            Result result = (Result) response;
            //记录日志
            //TODO 可以记录入库
            String msg = String.format("请求:%s======响应:%s======总耗时(毫秒):%s", JSON.toJSONString(request),
                    JSON.toJSONString(response), watch.getTotalTimeMillis());
    
            if (result.getStatus()== ResultCode.SUCCESS.getCode()) {
                log.info(msg);
            } else {
                log.error(msg);//记录错误日志
            }
    
            return response;
        }
    
    }
  • 相关阅读:
    android 管理wifi
    andriod 剪贴板操作
    andriod绘制图形
    andriod读写XML
    androd 获得wifi列表
    一个小时内学习SQLite数据库
    Android 使用SQLite本地数据库
    Android可伸缩列表ExpandableListView
    三甲医院涉医疗反腐,今年已有 149 位被查!
    2015各地区顶尖中学排行榜
  • 原文地址:https://www.cnblogs.com/july-sunny/p/12240199.html
Copyright © 2011-2022 走看看