使用自定义的注解做操作日志监控

在项目实践中,对于不同场景需要对用户的相关操作进行监控,如对后台人员的相关操作进行监控并进行相应的日志记录等,这个时候就会用上AOP,下面我讲根据自己在项目中遇到的实际情况进行描述和分析
使用场景:对用户的增删改查等操作进行记录,以达到监控的目的。
监控的级别可以到方法上面,也可以到某个类上面
本文主要阐述到方法级别的日志监控

首先自定义一个注解:

import java.lang.annotation.*;

@Target(value = ElementType.METHOD)
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
public @interface RecordLog {
    // 操作的类型:新增、删除、下架、上移下移、更新
    public String operationType();
}

自定义的注解是带有参数的,这个参数是用来区分不同的操作的(新增、删除、下架、上移下移、更新等操作)

然后在需要记录日志的地方使用该注解:


public class SwitchServiceImpl implements SwitchService {
...
    @RecordLog(operationType = Constants.OPERATION_MODIFY)
    @Override
    public Result setupSwitch(SwitchDTO dto) {
        ...
        return result;
    }
}

接着使用切片的方式当运行到该方法时,进行监控日志的记录

@Component
@Aspect
@Order(2)
public class OperationLogRecordService {

    private static final Logger logger = LoggerFactory.getLogger(OperationLogRecordService.class);

    @Autowired
    TLogMapper tLogMapper;

//    @Pointcut("execution(* com.*..*.*ServiceImpl.*(..))")
    @Pointcut("@annotation(com.util.RecordLog)")
    private void log() {
    }

    @Around("log()&&@annotation(recordLog)")
    public Object invoke(ProceedingJoinPoint joinPoint, RecordLog recordLog) throws Throwable {
        Object result = joinPoint.proceed();
        Result newResult = (Result) result;
        String operationIsSuccess = ((Result) result).getCode() == 0? "成功":"失败";

        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
        String path = method.getDeclaringClass().getPackage().getName();
        String className = method.getDeclaringClass().getSimpleName();
        String methodName = method.getName();
        Object[] args = joinPoint.getArgs();
        JSONArray argsJson = new JSONArray(args);
        String params = JSON.toJSONString(args);
        JSONObject jsonObject = null;
        if(argsJson != null)
            jsonObject = argsJson.getJSONObject(0);

        String operatorId = null;
        String temp = null;
        if(jsonObject.has("operatorId")) {
            temp = jsonObject.getString("operatorId");
            if (temp != null || !"".equals(temp)) {
                operatorId = temp;
            }
        }

        Integer type = null;
        String typeName = null;
        if(jsonObject.has("type")) {
            type = jsonObject.getInt("type");
            typeName = (type.intValue() == Constants.TYPE.getByType(type).getType()? Constants.TYPE.getByType(type).getName():"");
        }

        if(jsonObject.has("switchOnOrOff")) {
            typeName = "控制优先展示广告位还是直播";
        }
        TLog tLog = new TLog ();
        tLog.setClassname(path + "." + className);
        tLog.setMethodname(methodName);
        tLog.setOperatetime(new Date());
        tLog.setOperatorid(operatorId);
        tLog.setParam(JSON.toJSONString(args));
        tLog.setOpterationtype(recordLog.operationType());
        tLog.setType(typeName);
        tLog.setOperationissuccess(operationIsSuccess);

        int num = tLogMapper.insertSelective(tLog);
       if(num == 1)
           logger.info(new Date() + "操作日志记录成功!");
       else
           logger.info(new Date() + "操作日志记录失败!");
        return result;
    }
}

你可能感兴趣的:(Springboot)