aop全局 *** 作日志记录

aop全局 *** 作日志记录,第1张

aop全局 *** 作日志记录
@Aspect
@Component
public class GlobalLogAspect {

    
    private static final ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor(
            30,
            100,
            120L,
            //允许线程空闲时间和单位,超过了核心线程出之外的线程在空闲时间到达之后会被销毁
            TimeUnit.SECONDS,
            //指定一种队列 (有界队列 先进先出)
            new ArrayBlockingQueue<>(100),
            //默认线程工厂
            Executors.defaultThreadFactory(),
            //自定义拒绝策略
            new ThreadPoolExecutor.CallerRunsPolicy()
    );
    @Resource
    private DggMongoUtils dggMongoUtils;


    @Pointcut("@annotation(net.crisps.hr.middle.common.aop.Log)")
    public void controllerPoint(){
    }

    @Around("controllerPoint()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        Signature signature = pjp.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Object[] arguments = pjp.getArgs();
        Method method = methodSignature.getMethod();
        HrLog hrLog = new HrLog();
        if (method.isAnnotationPresent(Log.class)) {
            // 获取request
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
            HttpServletRequest request = servletRequestAttributes.getRequest();
            StringBuilder sb = new StringBuilder();
            sb.append(request.getRequestURI());
            sb.append(JSON.toJSonString(request.getParameterMap()));
            // 最佳请求参数
            hrLog.setParams(sb.toString());
            // 追加 *** 作用户Ip
            LoginUser loginUser = LoginUtils.getLoginUser();
            hrLog.setUserId(loginUser.getId());
            hrLog.setUserName(loginUser.getName());
            hrLog.setMchDetailId(loginUser.getMchDetailId());
            //  *** 作时间
            hrLog.setOperationTime(DggDateUtil.getCurrentDateTime());
            // pjp执行
            try {
                Object result = pjp.proceed();
                if (result instanceof CrispsResponse) {
                    // 返回对象
                    CrispsResponse mvResult = (CrispsResponse) result;
                    // 追加 返回对象
                    hrLog.setResult(JSON.toJSonString(mvResult));
                    hrLog.setHasException(0);
                }
                return result;
            } catch (Exception e) {
                hrLog.setHasException(1);
                hrLog.setResult(e.getMessage());
                // 打印错误日志
                DggLogUtil.error(e, request.getRequestURI(), JSON.toJSonString(request.getParameterMap()));
            } finally {
                Class[] clazzs = method.getParameterTypes();
                if (clazzs.length == arguments.length) {
                    // 获取注解信息
                    String operationType = method.getAnnotation(Log.class).operationType();
                    String operationName = method.getAnnotation(Log.class).operationName();
                    hrLog.setOperationType(operationType);
                    hrLog.setOperationName(operationName);
                    hrLog.setIp(ContextHolderUtils.getIp());
                    hrLog.setId(DggKeyWorker.nextId());
                }
                //使用多线程处理存储日志业务逻辑
                EXECUTOR.execute(()->{
                    dggMongoUtils.save(ConstantInfo.USER_OPERATE_LOG_COLLECTION,hrLog);
                });
            }
        }
        return pjp.proceed();
    }


}


@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@documented
public @interface Log {
    
    public String operationType() default "";

    
    public String operationName() default "";
}

最后在controller上加上

@Log(operationName = "查询手机号码", operationType = "select")注解即可

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5687182.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-17
下一篇2022-12-17

发表评论

登录后才能评论

评论列表(0条)

    保存