欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

AOP添加操作日志

程序员文章站 2024-03-02 19:14:52
...
@Aspect
@Component
@Slf4j
public class SysLogAspect {
    @Resource
    UserTokenBean userTokenBean;
    @Autowired
    private SystemLogController systemLogController;
    @Autowired
    private CommonDictController commonDictController;


    @Pointcut("@annotation(com.****.common.aop.SysLog)")
    public void sysLogAspect() {

    }


    @AfterReturning(pointcut = "sysLogAspect()", returning = "result")
    public void afterReturning(JoinPoint jp, AbstractApiResult result) {
        try {
            log.info("-----操作完成,开始增加日志-----");
            String name = userTokenBean.getRealName();
            SystemLogSaveDTO dto = new SystemLogSaveDTO();
            dto.setGmtUser(name);
            MethodSignature signature = (MethodSignature) jp.getSignature();
            //请求的 类名、方法名
            Method method = signature.getMethod();
            SysLog sysLog = method.getAnnotation(SysLog.class);
            if (StringUtils.isEmpty(sysLog.logType())) {
                throw new NullPointerException("日志类型参数不能为空");
            }
            CommonDictDTO logTypeQuery = new CommonDictDTO();
            logTypeQuery.setGroupId("1002");
            logTypeQuery.setCode(sysLog.logType());
            List<CommonDictDTO> opList = ThirdAnalysisUtils.maxrockyAnalysis(commonDictController.findByCommonDictDTO(logTypeQuery));
            CommonDictDTO cd2 = opList.get(0);
            dto.setLogType(cd2.getId());
            String data = (String) result.getData();
            dto.setContent(data);
            log.info("result-----------:[{}]", JSON.toJSONString(result));
            //设置操作类型
            CommonDictDTO commonDictListDto = new CommonDictDTO();
            commonDictListDto.setGroupId("1001");
            List<CommonDictDTO> commonDictDTOS = ThirdAnalysisUtils.maxrockyAnalysis(commonDictController.findByCommonDictDTO(commonDictListDto));
            for (CommonDictDTO commonDictDTO : commonDictDTOS) {
                if (data.contains(commonDictDTO.getDictionaryName())) {
                    dto.setOperationType(commonDictDTO.getId());
                }
            }
            if (StringUtils.isEmpty(dto.getOperationType())) {
                log.info("日志入库dto:[{}]", JSON.toJSONString(dto));
                throw new NullPointerException("操作类型参数不能为空");
            }
            systemLogController.saveOrUpdate(dto);
            log.info("-----操作完成,结束增加日志-----");
        } catch (Exception e) {
            log.error("AOP 获取请求参数 异常-------->", e);
        }
    }


}

相关标签: 项目开发 java