操作日志记录实战总结

本文旨在实现注解式操作日志记录:
实现步骤:
1、定义注解式接口:
OperationLog.java:

/**
 * 标记需要记录操作日志的方法
 */
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface OperationLog {

    /**
     * 业务名称,如:"修改雇员"
     */
    String value() default "";

    /**
     * 被修改的实体的唯一标识,如:雇员ID
     */
    String key() default "";

}

2、定义切面:
OptLogAop.java:

/**
 * 操作日志AOP
 */
@Aspect
@Component
public class OptLogAop {

    @Pointcut(value = "@annotation(com.test.log.OperationLog)")
    public void cutMothod() {
    }
    
    @AfterReturning(value = "cutMothod()", returning = "returnValue")
    public void afterHandle(JoinPoint point, DSResponse returnValue) throws Exception {
        // 获取拦截的方法
        Signature sig = point.getSignature();
        if (!(sig instanceof MethodSignature)) {
            throw new IllegalArgumentException("该注解只可用于方法!");
        }
        MethodSignature mSig = (MethodSignature) sig;
        Object target = point.getTarget();

        Method currentMethod = target.getClass().getMethod(mSig.getName(), mSig.getParameterTypes());

        // 若当前用户未登录,则不记录操作日志
        User user = AuthUtils.getCurrentUser();
        if (user == null) {
            return;
        }

        // 获取操作名称
        OperationLog annotation = currentMethod.getAnnotation(OperationLog.class);
        String key = annotation.key();

        String[] paramNames = mSig.getParameterNames();
        StringBuilder sb = new StringBuilder();
        Object paramValue = null;

        if (!QString.isNullOrEmpty(key) && paramNames.length != 0) {
            for (int i = 0; i < paramNames.length; i++) {
                if (key.equals(paramNames[i])) {
                    paramValue = point.getArgs()[i];
                }
            }
            sb.append(key + ":" + paramValue);
        }
        String optBus = annotation.value();
        String ipAddress = ApUtil.getClientIp(ApUtil.getRequest());
        // 船舰
        OptLogFactory.createOptLog(user, optBus, sb.toString(), returnValue, ipAddress);
    }
}

3、创建日志记录,保存至数据库:
OptLogFactory.java:

public class OptLogFactory {

    private static OptLogService optLogService = AppContext.getBean(OptLogService.class);
    private static Logger logger = LoggerFactory.getLogger(OptLogFactory.class);

    public static void createOptLog(User user, String optBus, String optKeyWord,
                                    DSResponse returnValue, String ipAddress) {
        OptLog optLog = new OptLog();
        try {
            optLog.setOptUserName(user.getUserName());
            optLog.setOptUserDept(user.getDeptName());
            optLog.setOptBus(optBus);
            optLog.setIpAddress(ipAddress);
            optLog.setOptKeyWord(optKeyWord);
            optLog.setOptResult(returnValue.getStatus());
            optLog.setOptTime(System.currentTimeMillis());
            optLog.setRemarks(StringUtils.join(returnValue.getMessage(), ","));

            optLogService.save(optLog);
        } catch (Exception e) {
            logger.error("创建操作日志异常!", e);
        }
    }
}

4、注解的使用:
在涉及到增删改的方法上添加@OperationLog注解即可。

@OperationLog(value = "修改用户", key = "userId")
public DSResponse updateUser(String userId){
	···
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值