注解+AOP实现日志

通知类

点击查看代码
@Component
@Aspect
@Slf4j
public class OperateAdvice {

    @Autowired
    private InfoFromTokenBiz infoFromTokenBiz;

    @Pointcut("within(com.yishan.controller.*)")
    public void pointcut() {
    }


    @Around("pointcut() && @annotation(operateLog)")
    public void doAround(ProceedingJoinPoint pjp, OperateLog operateLog) throws Throwable {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            HttpServletRequest request = attributes.getRequest();
            IJWTInfo ijwtInfo = infoFromTokenBiz.getUserinFormation(request);
            String workerUserId = ijwtInfo.getWorkerUserId();
            String requestTime = sdf.format(new Date());
            String className = pjp.getTarget().getClass().getName();
            String methodName = pjp.getSignature().getName();
            String argsStr = Arrays.toString(pjp.getArgs());

            long start_time = System.currentTimeMillis();

            Object object = pjp.proceed();

            long end_time = System.currentTimeMillis();
            long timeConsuming = end_time - start_time;
            String returnType;
            String returnValue;
            if (object != null) {
                returnType = object.getClass().getName();
                returnValue = object.toString();
            } else {
                returnType = "java.lang.Object";
                returnValue = "void";
            }

            log.error("请求拦截-{}-{}-{}-{}-{}-{}-{}-{}", requestTime, workerUserId, className, methodName, argsStr, timeConsuming, returnType, returnValue);

        }

    }


    /* *//**
     * 前置通知:在连接点之前执行的通知
     *
     * @param joinPoint
     * @throws Throwable
     *//*
    @Before("pointcut() && @annotation(operateLog)")
    public void doBefore(JoinPoint joinPoint, OperateLog operateLog) throws Throwable {
        // 接收到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            log.info("请求前-{}-{}-{}-{}-{}-{}-{}", request.getRequestURL(), request.getMethod(),
                    readAsParam(request), joinPoint.getSignature().getDeclaringTypeName(),
                    joinPoint.getSignature().getName(), Arrays.toString(joinPoint.getArgs()));
        }
    }

    @AfterReturning(returning = "ret", pointcut = "pointcut() && @annotation(operateLog))")
    public void doAfterReturning(JoinPoint joinPoint, Object ret, OperateLog operateLog) throws Throwable {
        // 接收到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            log.info("请求后-{}-{}-{}-{}-{}-{}-{}=={}", request.getRequestURL(), request.getMethod(),
                    readAsParam(request), joinPoint.getSignature().getDeclaringTypeName(),
                    joinPoint.getSignature().getName(), Arrays.toString(joinPoint.getArgs()), ret);
        }
    }

    public static Map<String, String> readAsParam(HttpServletRequest request) {
        Map<String, String> map = new HashMap<>(8);
        Map<String, String[]> parameterMap = request.getParameterMap();
        parameterMap.entrySet().iterator().forEachRemaining(param -> {
            String key = param.getKey();
            String[] value = param.getValue();
            map.put(key, Arrays.toString(value));
        });
        return map;

    }*/

}

自定义注解

点击查看代码
package com.highqi.security.admin.config.operateLog;

import java.lang.annotation.*;

/**
 * 自定义日志注解
 *
 * @author: yishan
 * @Date: 2022/9/13 10:03
 **/
@Inherited
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface OperateLog {
}

posted @ 2022-09-13 11:32  yishan99  阅读(32)  评论(0)    收藏  举报