自定义注解aop实现访问接口log打印

maven

        <!-- aop依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        //配置
  aop:
    proxy-target-class: true

1.自定义注解

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

2.aop实现类

@Slf4j
@Aspect
@Component
public class LogAspect {

    @Resource
    private BaseProperties dsProperties;
    @Resource
    private LogService logService;

    @Around("@annotation(com.gc.jgdq.cowork.common.annotation.Log)")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        Object result = null;
        // 获取 request
        HttpServletRequest request = HttpContextUtil.getHttpServletRequest();
        String uri = request.getRequestURI();
        String method = request.getMethod();
        log.info("********************【" + uri + "】 请求开始 **************************");
        log.info("【请求路径】>>>> {}", uri);
        log.info("【请求方式】>>>> {}", method);
        log.info("【方法参数】>>>> {}", getFieldsName((JoinPoint) point));
        long beginTime = System.currentTimeMillis();
        try {
            // 执行方法
            result = point.proceed();
        } catch (Throwable e) {
            log.error(e.getMessage());
            throw e;
        }

        // 设置 IP 地址
        String ip = IpUtil.getIpAddr(request);
        // 执行时长(毫秒)
        long time = System.currentTimeMillis() - beginTime;

        if (dsProperties.isOpenAopLog()) {
            // 保存日志
            String token = (String) SecurityUtils.getSubject().getPrincipal();
            String username = JWTUtil.getUsername(token);

            SysLog log = new SysLog();
            log.setId(UIdUtil.getInstance().id());
            log.setUsername(username);
            log.setIp(ip);
            log.setTime(time);
            logService.saveLog(point, log);
        }

        log.info("【请求耗时】>>>> {} ms", time);
//        log.info("【响应结果】>>>> {}", result);
        log.info("********************【" + uri + "】 请求结束 **************************");
        return result;
    }

    private Map<String, Object> getFieldsName(JoinPoint joinPoint) throws ClassNotFoundException, NoSuchMethodException {
        String classType = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        // 参数值
        Object[] args = joinPoint.getArgs();
        Class<?>[] classes = new Class[args.length];
        for (int k = 0; k < args.length; k++) {
            // 如果方法参数出现null,直接返回null 不做处理
            if (args[k] == null) {
                return null;
            }
            // 对于接受参数中含有MultipartFile,ServletRequest,ServletResponse类型的特殊处理,我这里是直接返回了null。(如果不对这三种类型判断,会报异常)
            if (args[k] instanceof MultipartFile || args[k] instanceof ServletRequest || args[k] instanceof ServletResponse) {
                return null;
            }
            if (!args[k].getClass().isPrimitive()) {
                // 当方法参数是封装类型
                Class s = args[k].getClass();

                classes[k] = s == null ? args[k].getClass() : s;
            }
        }
        ParameterNameDiscoverer pnd = new DefaultParameterNameDiscoverer();
        // 获取指定的方法,第二个参数可以不传,但是为了防止有重载的现象,还是需要传入参数的类型
        Method method = Class.forName(classType).getMethod(methodName, classes);
        // 参数名
        String[] parameterNames = pnd.getParameterNames(method);
        // 通过map封装参数和参数值
        HashMap<String, Object> paramMap = new HashMap<>();
        assert parameterNames != null;
        for (int i = 0; i < parameterNames.length; i++) {
            paramMap.put(parameterNames[i], args[i]);
        }
        return paramMap;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值