1. 创建日志表
CREATE TABLE `dyyy_operation_log` (
`id` bigint NOT NULL COMMENT '主键',
`operation_id` bigint DEFAULT NULL COMMENT '操作id',
`user_id` bigint DEFAULT NULL COMMENT '用户id',
`user_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '用户名称',
`module` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '操作模块',
`method` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '操作方法',
`ip` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '操作人客户单ip地址',
`params` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci COMMENT '参数',
`operation_desc` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '描述',
`create_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='操作日志表';
2.新增日志注解
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface SysLog { String MODULE() default "操作模块"; String REMARK() default "操作日志"; }
3.新增AOP 切面
@Aspect @Component @Slf4j @Order(1) public class OperationLogAop { @Autowired private OperationLogService operationLogService; public OperationLogAop() { log.info("初始化接口日志切面类..."); } @Pointcut(value = "@annotation(com.dycx.dyyy.web.config.SysLog)") public void controllerInterceptor() { } @Around("controllerInterceptor()") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { Object result = joinPoint.proceed(); try { handle(joinPoint); } catch (Exception e) { log.error("日志记录出错!", e); } return result; } private void handle(ProceedingJoinPoint joinPoint) throws Exception { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); SysLog sysLog = method.getAnnotation(SysLog.class); OperationLog operationLog = new OperationLog(); if (sysLog != null) { operationLog.setModule(sysLog.MODULE()); operationLog.setMethod(sysLog.REMARK()); operationLog.setOperationType(sysLog.TYPE()); } //请求的方法名 String clazzName = joinPoint.getTarget().getClass().getName(); Class<?> clazz = Class.forName(clazzName); String clazzSimpleName = clazz.getSimpleName(); String methodName = signature.getName(); operationLog.setOperationDesc(clazzSimpleName + "." + methodName); //请求的参数 String[] parameterNames = ((MethodSignature) joinPoint.getSignature()).getParameterNames(); StringBuilder sb = null; if (Objects.nonNull(parameterNames)) { sb = new StringBuilder(); for (int i = 0; i < parameterNames.length; i++) { Object param = joinPoint.getArgs()[i] != null ? joinPoint.getArgs()[i] : ""; if (StringUtils.isNotEmpty(param.toString()) && !"request".equals(parameterNames[i]) && !"response".equals(parameterNames[i]) && !"modelMap".equals(parameterNames[i])) { if (param instanceof Integer) { sb.append(parameterNames[i] + ":" + param + "; "); } else if (param instanceof String) { sb.append(parameterNames[i] + ":" + param + "; "); } else if (param instanceof Double) { sb.append(parameterNames[i] + ":" + param + "; "); } else if (param instanceof Float) { sb.append(parameterNames[i] + ":" + param + "; "); } else if (param instanceof Long) { sb.append(parameterNames[i] + ":" + param + "; "); } else if (param instanceof Boolean) { sb.append(parameterNames[i] + ":" + param + "; "); } else if (param instanceof Date) { sb.append(parameterNames[i] + ":" + param + "; "); } else if (param instanceof Timestamp) { sb.append(parameterNames[i] + ":" + param + "; "); } else { sb.append(parameterNames[i] + ":" + getString(param) + "; "); } } } } sb = sb == null ? new StringBuilder() : sb; operationLog.setParams(sb.toString()); HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); operationLog.setIp(IpUtil.getIpAddr(request)); UserInfo login = (UserInfo) SecurityUtils.getSubject().getPrincipal(); operationLog.setUserName(login.getName()); operationLog.setUserId(login.getId()); operationLog.setCreateTime(LocalDateTime.now()); operationLogService.addOne(operationLog); } }
4.添加日志记录
@SysLog(MODULE = "用户模块", REMARK = "查看用户手机号,身份证信息")