拦截类UserLogAopAspect
@Slf4j
@Aspect
@Component
@Order(AopOrderConst.LOG_AOP_ORDER)
public class UserLogAopAspect {
@Autowired
private IUserOperateLogService userOperateLogService;
@Autowired
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
@Around("@annotation(annotation)")
public Object logSave(ProceedingJoinPoint joinPoint, UserOperateLogAnnotation annotation) throws Throwable {
long start = System.currentTimeMillis();
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
//调用原来的方法
Object result = joinPoint.proceed();
UserOperateLogAnnotation operateLogAnnotation = methodSignature.getMethod().getDeclaredAnnotation(UserOperateLogAnnotation.class);
//如果需要记录数据库开启异步操作
if (Objects.nonNull(operateLogAnnotation) && operateLogAnnotation.saveDb()) {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder
.getRequestAttributes();
HttpServletRequest request = servletRequestAttributes.getRequest();
// 请求流水号
String traceId = StringUtils.defaultString(TraceUtils.getTrace(), MDC.get(TraceConstant.LOG_TRACE_ID));
String httpMethod = request.getMethod();
List
注解类 UserOperateLogAnnotation
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface UserOperateLogAnnotation {
/**
* 模块
* @return
*/
String module() default "";
/**
* 是否保存到数据库
* @return
*/
boolean saveDb() default false;
}
controller调用
@PostMapping("/updateUser")
@ApiOperation(value = "修改用户", notes = "修改用户")
@UserOperateLogAnnotation(module = "admin", saveDb = true)
public ResponseMessage> updateUser(@RequestBody UpdateCompanyDto updateCompanyDto) {
return null;
}
创建SQL脚本
CREATE TABLE `user_operate_log` (
`id` bigint NOT NULL COMMENT '主键ID',
`username` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '用户名',
`module` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '模块名',
`service` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '服务',
`operate_type` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '操作类型',
`params` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL COMMENT '入参参数值',
`remark` varchar(1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '备注',
`success` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '是否成功',
`create_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = DYNAMIC;