栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

aop全局操作日志记录

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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")注解即可
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/686653.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号