利用aop实现网站日志记录方案

1定义注解

package com.xxx.common.log;

import java.lang.annotation.*;

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface UserOperateLog{
    /**
     * 模块
     */
    String module();

    /**
     * 业务功能
     */
    String businessType();

    /**
     * 是否保存请求参数
     */
    boolean isSaveRequestData() default true;

    /**
     * 是否保存响应数据
     */
    boolean isSaveResponseData() default true;
}

2 设置aop

package com.xxx.core.aop;



import java.util.concurrent.Executor;


@Aspect
public class UserOperateLogAspect extends AbstractOperateLog {
    private Executor defaultExecutor;

    private UserOperateLogService userOperateLogService;

    private ThreadLocal<Long> startMs = new ThreadLocal<>();

    public UserOperateLogAspect(Executor defaultExecutor, UserOperateLogService userOperateLogService) {
        this.defaultExecutor = defaultExecutor;
        this.userOperateLogService = userOperateLogService;
    }

    @Before("@annotation(com.chint.common.log.UserOperateLog)")
    public void before() {
        startMs.set(System.currentTimeMillis());
    }

    @AfterReturning(value = "@annotation(userOperateLog)", returning = "resultUtil")
    public void doAfterReturning(JoinPoint joinPoint, UserOperateLog userOperateLog, ResultUtil<?> resultUtil) {
        this.sendToMessageQueue(joinPoint, userOperateLog, resultUtil);
    }

    @AfterThrowing(value = "@annotation(userOperateLog)")
    public void doAfterThrowing(JoinPoint joinPoint, UserOperateLog userOperateLog) {
        this.sendToMessageQueue(joinPoint, userOperateLog, null);
    }

    private void sendToMessageQueue(JoinPoint joinPoint, UserOperateLog userOperateLog, ResultUtil<?> resultUtil) {
        final UserOperateLogDO operateLogDO = new UserOperateLogDO();
        final Object currentUser = ServletUtil.getCurrentUser();
        if (currentUser instanceof UserDO) {
            operateLogDO.setUserId(((UserDO) currentUser).getId());
        }
        operateLogDO.setModule(userOperateLog.module());
        operateLogDO.setBusinessType(userOperateLog.businessType());
        operateLogDO.setResponseStatus(resultUtil != null ? resultUtil.getStatus() : 400);
        assignment(operateLogDO, joinPoint, resultUtil, userOperateLog.isSaveRequestData(), userOperateLog.isSaveResponseData());
        operateLogDO.setExecuteMs((int) (System.currentTimeMillis() - startMs.get()));
        defaultExecutor.execute(() -> userOperateLogService.sendToMessageQueue(operateLogDO));
        startMs.remove();
    }

}

3 在需要的接口上标注注解


    @PutMapping("company/brand")
    @ApiOperation("更新商家商标")
    @UserOperateLog(module = CompanyModule.MODULE, businessType = CompanyModule.BusinessType.COMPANY_BRAND)
    public ResultUtil<MessageEnum> updateCompanyBrand(@Validated UpdateCompanyBrandQuery query) {
        companyService.updateCompanyBrand(query);
        return ResultUtil.success(MessageEnum.UPDATE_SUCCESS);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值