Commit 15e2c0945d448c72c78ed59911f898a96abe74f8

Authored by YunaiV
1 parent c81c275a

初始化访问日志、异常日志的插入

Showing 14 changed files with 400 additions and 6 deletions
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/infra/service/logger/impl/InfApiAccessLogServiceImpl.java
@@ -32,10 +32,9 @@ public class InfApiAccessLogServiceImpl implements InfApiAccessLogService { @@ -32,10 +32,9 @@ public class InfApiAccessLogServiceImpl implements InfApiAccessLogService {
32 @Override 32 @Override
33 @Async 33 @Async
34 public Future<Boolean> createApiAccessLogAsync(ApiAccessLogCreateDTO createDTO) { 34 public Future<Boolean> createApiAccessLogAsync(ApiAccessLogCreateDTO createDTO) {
35 - // 插入  
36 InfApiAccessLogDO apiAccessLog = InfApiAccessLogConvert.INSTANCE.convert(createDTO); 35 InfApiAccessLogDO apiAccessLog = InfApiAccessLogConvert.INSTANCE.convert(createDTO);
37 int insert = apiAccessLogMapper.insert(apiAccessLog); 36 int insert = apiAccessLogMapper.insert(apiAccessLog);
38 - return new AsyncResult<>(insert == 1); 37 + return new AsyncResult<>(insert > 1);
39 } 38 }
40 39
41 @Override 40 @Override
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/convert/logger/InfApiAccessLogConvert.java 0 → 100644
  1 +package cn.iocoder.yudao.userserver.modules.infra.convert.logger;
  2 +
  3 +import cn.iocoder.yudao.framework.apilog.core.service.dto.ApiAccessLogCreateDTO;
  4 +import cn.iocoder.yudao.userserver.modules.infra.dal.dataobject.logger.InfApiAccessLogDO;
  5 +import org.mapstruct.Mapper;
  6 +import org.mapstruct.factory.Mappers;
  7 +
  8 +/**
  9 + * API 访问日志 Convert
  10 + *
  11 + * @author 芋道源码
  12 + */
  13 +@Mapper
  14 +public interface InfApiAccessLogConvert {
  15 +
  16 + InfApiAccessLogConvert INSTANCE = Mappers.getMapper(InfApiAccessLogConvert.class);
  17 +
  18 + InfApiAccessLogDO convert(ApiAccessLogCreateDTO bean);
  19 +
  20 +}
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/convert/logger/InfApiErrorLogConvert.java 0 → 100644
  1 +package cn.iocoder.yudao.userserver.modules.infra.convert.logger;
  2 +
  3 +import cn.iocoder.yudao.framework.apilog.core.service.dto.ApiErrorLogCreateDTO;
  4 +import cn.iocoder.yudao.userserver.modules.infra.dal.dataobject.logger.InfApiErrorLogDO;
  5 +import org.mapstruct.Mapper;
  6 +import org.mapstruct.factory.Mappers;
  7 +
  8 +/**
  9 + * API 错误日志 Convert
  10 + *
  11 + * @author 芋道源码
  12 + */
  13 +@Mapper
  14 +public interface InfApiErrorLogConvert {
  15 +
  16 + InfApiErrorLogConvert INSTANCE = Mappers.getMapper(InfApiErrorLogConvert.class);
  17 +
  18 + InfApiErrorLogDO convert(ApiErrorLogCreateDTO bean);
  19 +
  20 +}
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/convert/package-info.java 0 → 100644
  1 +/**
  2 + * 提供 POJO 类的实体转换
  3 + *
  4 + * 目前使用 MapStruct 框架
  5 + */
  6 +package cn.iocoder.yudao.userserver.modules.infra.convert;
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/convert/《芋道 Spring Boot 对象转换 MapStruct 入门》.md 0 → 100644
  1 +<http://www.iocoder.cn/Spring-Boot/MapStruct/?yudao>
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/dal/dataobject/logger/InfApiAccessLogDO.java 0 → 100644
  1 +package cn.iocoder.yudao.userserver.modules.infra.dal.dataobject.logger;
  2 +
  3 +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
  4 +import cn.iocoder.yudao.framework.common.pojo.CommonResult;
  5 +import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
  6 +import com.baomidou.mybatisplus.annotation.TableId;
  7 +import com.baomidou.mybatisplus.annotation.TableName;
  8 +import lombok.*;
  9 +
  10 +import java.util.Date;
  11 +
  12 +/**
  13 + * API 访问日志
  14 + *
  15 + * @author 芋道源码
  16 + */
  17 +@TableName("inf_api_access_log")
  18 +@Data
  19 +@EqualsAndHashCode(callSuper = true)
  20 +@ToString(callSuper = true)
  21 +@Builder
  22 +@NoArgsConstructor
  23 +@AllArgsConstructor
  24 +public class InfApiAccessLogDO extends BaseDO {
  25 +
  26 + /**
  27 + * 编号
  28 + */
  29 + @TableId
  30 + private Integer id;
  31 + /**
  32 + * 链路追踪编号
  33 + *
  34 + * 一般来说,通过链路追踪编号,可以将访问日志,错误日志,链路追踪日志,logger 打印日志等,结合在一起,从而进行排错。
  35 + */
  36 + private String traceId;
  37 + /**
  38 + * 用户编号
  39 + */
  40 + private Long userId;
  41 + /**
  42 + * 用户类型
  43 + *
  44 + * 枚举 {@link UserTypeEnum}
  45 + */
  46 + private Integer userType;
  47 + /**
  48 + * 应用名
  49 + *
  50 + * 目前读取 `spring.application.name` 配置项
  51 + */
  52 + private String applicationName;
  53 +
  54 + // ========== 请求相关字段 ==========
  55 +
  56 + /**
  57 + * 请求方法名
  58 + */
  59 + private String requestMethod;
  60 + /**
  61 + * 访问地址
  62 + */
  63 + private String requestUrl;
  64 + /**
  65 + * 请求参数
  66 + *
  67 + * query: Query String
  68 + * body: Quest Body
  69 + */
  70 + private String requestParams;
  71 + /**
  72 + * 用户 IP
  73 + */
  74 + private String userIp;
  75 + /**
  76 + * 浏览器 UA
  77 + */
  78 + private String userAgent;
  79 +
  80 + // ========== 执行相关字段 ==========
  81 +
  82 + /**
  83 + * 开始请求时间
  84 + */
  85 + private Date beginTime;
  86 + /**
  87 + * 结束请求时间
  88 + */
  89 + private Date endTime;
  90 + /**
  91 + * 执行时长,单位:毫秒
  92 + */
  93 + private Integer duration;
  94 + /**
  95 + * 结果码
  96 + *
  97 + * 目前使用的 {@link CommonResult#getCode()} 属性
  98 + */
  99 + private Integer resultCode;
  100 + /**
  101 + * 结果提示
  102 + *
  103 + * 目前使用的 {@link CommonResult#getMsg()} 属性
  104 + */
  105 + private String resultMsg;
  106 +
  107 +}
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/dal/dataobject/logger/InfApiErrorLogDO.java 0 → 100644
  1 +package cn.iocoder.yudao.userserver.modules.infra.dal.dataobject.logger;
  2 +
  3 +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
  4 +import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
  5 +import cn.iocoder.yudao.userserver.modules.infra.enums.logger.InfApiErrorLogProcessStatusEnum;
  6 +import com.baomidou.mybatisplus.annotation.TableName;
  7 +import lombok.*;
  8 +
  9 +import java.util.Date;
  10 +
  11 +/**
  12 + * API 异常数据
  13 + *
  14 + * @author 芋道源码
  15 + */
  16 +@TableName("inf_api_error_log")
  17 +@Data
  18 +@EqualsAndHashCode(callSuper = true)
  19 +@ToString(callSuper = true)
  20 +@Builder
  21 +@NoArgsConstructor
  22 +@AllArgsConstructor
  23 +public class InfApiErrorLogDO extends BaseDO {
  24 +
  25 + /**
  26 + * 编号
  27 + */
  28 + private Long id;
  29 + /**
  30 + * 用户编号
  31 + */
  32 + private Long userId;
  33 + /**
  34 + * 链路追踪编号
  35 + *
  36 + * 一般来说,通过链路追踪编号,可以将访问日志,错误日志,链路追踪日志,logger 打印日志等,结合在一起,从而进行排错。
  37 + */
  38 + private String traceId;
  39 + /**
  40 + * 用户类型
  41 + *
  42 + * 枚举 {@link UserTypeEnum}
  43 + */
  44 + private Integer userType;
  45 + /**
  46 + * 应用名
  47 + *
  48 + * 目前读取 spring.application.name
  49 + */
  50 + private String applicationName;
  51 +
  52 + // ========== 请求相关字段 ==========
  53 +
  54 + /**
  55 + * 请求方法名
  56 + */
  57 + private String requestMethod;
  58 + /**
  59 + * 访问地址
  60 + */
  61 + private String requestUrl;
  62 + /**
  63 + * 请求参数
  64 + *
  65 + * query: Query String
  66 + * body: Quest Body
  67 + */
  68 + private String requestParams;
  69 + /**
  70 + * 用户 IP
  71 + */
  72 + private String userIp;
  73 + /**
  74 + * 浏览器 UA
  75 + */
  76 + private String userAgent;
  77 +
  78 + // ========== 异常相关字段 ==========
  79 +
  80 + /**
  81 + * 异常发生时间
  82 + */
  83 + private Date exceptionTime;
  84 + /**
  85 + * 异常名
  86 + *
  87 + * {@link Throwable#getClass()} 的类全名
  88 + */
  89 + private String exceptionName;
  90 + /**
  91 + * 异常导致的消息
  92 + *
  93 + * {@link cn.hutool.core.exceptions.ExceptionUtil#getMessage(Throwable)}
  94 + */
  95 + private String exceptionMessage;
  96 + /**
  97 + * 异常导致的根消息
  98 + *
  99 + * {@link cn.hutool.core.exceptions.ExceptionUtil#getRootCauseMessage(Throwable)}
  100 + */
  101 + private String exceptionRootCauseMessage;
  102 + /**
  103 + * 异常的栈轨迹
  104 + *
  105 + * {@link org.apache.commons.lang3.exception.ExceptionUtils#getStackTrace(Throwable)}
  106 + */
  107 + private String exceptionStackTrace;
  108 + /**
  109 + * 异常发生的类全名
  110 + *
  111 + * {@link StackTraceElement#getClassName()}
  112 + */
  113 + private String exceptionClassName;
  114 + /**
  115 + * 异常发生的类文件
  116 + *
  117 + * {@link StackTraceElement#getFileName()}
  118 + */
  119 + private String exceptionFileName;
  120 + /**
  121 + * 异常发生的方法名
  122 + *
  123 + * {@link StackTraceElement#getMethodName()}
  124 + */
  125 + private String exceptionMethodName;
  126 + /**
  127 + * 异常发生的方法所在行
  128 + *
  129 + * {@link StackTraceElement#getLineNumber()}
  130 + */
  131 + private Integer exceptionLineNumber;
  132 +
  133 + // ========== 处理相关字段 ==========
  134 +
  135 + /**
  136 + * 处理状态
  137 + *
  138 + * 枚举 {@link InfApiErrorLogProcessStatusEnum}
  139 + */
  140 + private Integer processStatus;
  141 + /**
  142 + * 处理时间
  143 + */
  144 + private Date processTime;
  145 + /**
  146 + * 处理用户编号
  147 + *
  148 + * 关联 cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.user.SysUserDO.SysUserDO#getId()
  149 + */
  150 + private Long processUserId;
  151 +
  152 +}
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/dal/mysql/logger/InfApiAccessLogMapper.java 0 → 100644
  1 +package cn.iocoder.yudao.userserver.modules.infra.dal.mysql.logger;
  2 +
  3 +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
  4 +import cn.iocoder.yudao.userserver.modules.infra.dal.dataobject.logger.InfApiAccessLogDO;
  5 +import org.apache.ibatis.annotations.Mapper;
  6 +
  7 +import java.util.List;
  8 +
  9 +/**
  10 + * API 访问日志 Mapper
  11 + *
  12 + * @author 芋道源码
  13 + */
  14 +@Mapper
  15 +public interface InfApiAccessLogMapper extends BaseMapperX<InfApiAccessLogDO> {
  16 +}
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/dal/mysql/logger/InfApiErrorLogMapper.java 0 → 100644
  1 +package cn.iocoder.yudao.userserver.modules.infra.dal.mysql.logger;
  2 +
  3 +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
  4 +import cn.iocoder.yudao.userserver.modules.infra.dal.dataobject.logger.InfApiErrorLogDO;
  5 +import org.apache.ibatis.annotations.Mapper;
  6 +
  7 +/**
  8 + * API 错误日志 Mapper
  9 + *
  10 + * @author 芋道源码
  11 + */
  12 +@Mapper
  13 +public interface InfApiErrorLogMapper extends BaseMapperX<InfApiErrorLogDO> {
  14 +}
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/enums/logger/InfApiErrorLogProcessStatusEnum.java 0 → 100644
  1 +package cn.iocoder.yudao.userserver.modules.infra.enums.logger;
  2 +
  3 +import lombok.AllArgsConstructor;
  4 +import lombok.Getter;
  5 +
  6 +/**
  7 + * API 异常数据的处理状态
  8 + *
  9 + * @author 芋道源码
  10 + */
  11 +@AllArgsConstructor
  12 +@Getter
  13 +public enum InfApiErrorLogProcessStatusEnum {
  14 +
  15 + INIT(0, "未处理"),
  16 + DONE(1, "已处理"),
  17 + IGNORE(2, "已忽略");
  18 +
  19 + /**
  20 + * 状态
  21 + */
  22 + private final Integer status;
  23 + /**
  24 + * 资源类型名
  25 + */
  26 + private final String name;
  27 +
  28 +}
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/enums/package-info.java 0 → 100644
  1 +/**
  2 + * 占位类,可以无视
  3 + */
  4 +package cn.iocoder.yudao.userserver.modules.infra.enums;
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/service/logger/impl/InfApiAccessLogServiceImpl.java
1 package cn.iocoder.yudao.userserver.modules.infra.service.logger.impl; 1 package cn.iocoder.yudao.userserver.modules.infra.service.logger.impl;
2 2
3 import cn.iocoder.yudao.framework.apilog.core.service.dto.ApiAccessLogCreateDTO; 3 import cn.iocoder.yudao.framework.apilog.core.service.dto.ApiAccessLogCreateDTO;
  4 +import cn.iocoder.yudao.userserver.modules.infra.convert.logger.InfApiAccessLogConvert;
  5 +import cn.iocoder.yudao.userserver.modules.infra.dal.dataobject.logger.InfApiAccessLogDO;
  6 +import cn.iocoder.yudao.userserver.modules.infra.dal.mysql.logger.InfApiAccessLogMapper;
4 import cn.iocoder.yudao.userserver.modules.infra.service.logger.InfApiAccessLogService; 7 import cn.iocoder.yudao.userserver.modules.infra.service.logger.InfApiAccessLogService;
5 import lombok.extern.slf4j.Slf4j; 8 import lombok.extern.slf4j.Slf4j;
6 import org.springframework.scheduling.annotation.AsyncResult; 9 import org.springframework.scheduling.annotation.AsyncResult;
7 import org.springframework.stereotype.Service; 10 import org.springframework.stereotype.Service;
8 import org.springframework.validation.annotation.Validated; 11 import org.springframework.validation.annotation.Validated;
9 12
  13 +import javax.annotation.Resource;
10 import java.util.concurrent.Future; 14 import java.util.concurrent.Future;
11 15
12 /** 16 /**
@@ -19,9 +23,14 @@ import java.util.concurrent.Future; @@ -19,9 +23,14 @@ import java.util.concurrent.Future;
19 @Slf4j 23 @Slf4j
20 public class InfApiAccessLogServiceImpl implements InfApiAccessLogService { 24 public class InfApiAccessLogServiceImpl implements InfApiAccessLogService {
21 25
  26 + @Resource
  27 + private InfApiAccessLogMapper apiAccessLogMapper;
  28 +
22 @Override 29 @Override
23 public Future<Boolean> createApiAccessLogAsync(ApiAccessLogCreateDTO createDTO) { 30 public Future<Boolean> createApiAccessLogAsync(ApiAccessLogCreateDTO createDTO) {
24 - log.debug("{}", createDTO);  
25 - return new AsyncResult<>(true); 31 + InfApiAccessLogDO apiAccessLog = InfApiAccessLogConvert.INSTANCE.convert(createDTO);
  32 + int insert = apiAccessLogMapper.insert(apiAccessLog);
  33 + return new AsyncResult<>(insert > 1);
26 } 34 }
  35 +
27 } 36 }
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/service/logger/impl/InfApiErrorLogServiceImpl.java
1 package cn.iocoder.yudao.userserver.modules.infra.service.logger.impl; 1 package cn.iocoder.yudao.userserver.modules.infra.service.logger.impl;
2 2
3 import cn.iocoder.yudao.framework.apilog.core.service.dto.ApiErrorLogCreateDTO; 3 import cn.iocoder.yudao.framework.apilog.core.service.dto.ApiErrorLogCreateDTO;
  4 +import cn.iocoder.yudao.userserver.modules.infra.convert.logger.InfApiErrorLogConvert;
  5 +import cn.iocoder.yudao.userserver.modules.infra.dal.dataobject.logger.InfApiErrorLogDO;
  6 +import cn.iocoder.yudao.userserver.modules.infra.dal.mysql.logger.InfApiErrorLogMapper;
  7 +import cn.iocoder.yudao.userserver.modules.infra.enums.logger.InfApiErrorLogProcessStatusEnum;
4 import cn.iocoder.yudao.userserver.modules.infra.service.logger.InfApiErrorLogService; 8 import cn.iocoder.yudao.userserver.modules.infra.service.logger.InfApiErrorLogService;
5 import lombok.extern.slf4j.Slf4j; 9 import lombok.extern.slf4j.Slf4j;
6 import org.springframework.scheduling.annotation.AsyncResult; 10 import org.springframework.scheduling.annotation.AsyncResult;
7 import org.springframework.stereotype.Service; 11 import org.springframework.stereotype.Service;
8 import org.springframework.validation.annotation.Validated; 12 import org.springframework.validation.annotation.Validated;
9 13
  14 +import javax.annotation.Resource;
10 import java.util.concurrent.Future; 15 import java.util.concurrent.Future;
11 16
12 /** 17 /**
@@ -19,9 +24,15 @@ import java.util.concurrent.Future; @@ -19,9 +24,15 @@ import java.util.concurrent.Future;
19 @Slf4j 24 @Slf4j
20 public class InfApiErrorLogServiceImpl implements InfApiErrorLogService { 25 public class InfApiErrorLogServiceImpl implements InfApiErrorLogService {
21 26
  27 + @Resource
  28 + private InfApiErrorLogMapper apiErrorLogMapper;
  29 +
22 @Override 30 @Override
23 public Future<Boolean> createApiErrorLogAsync(ApiErrorLogCreateDTO createDTO) { 31 public Future<Boolean> createApiErrorLogAsync(ApiErrorLogCreateDTO createDTO) {
24 - log.debug("{}", createDTO);  
25 - return new AsyncResult<>(true); 32 + InfApiErrorLogDO apiErrorLog = InfApiErrorLogConvert.INSTANCE.convert(createDTO);
  33 + apiErrorLog.setProcessStatus(InfApiErrorLogProcessStatusEnum.INIT.getStatus());
  34 + int insert = apiErrorLogMapper.insert(apiErrorLog);
  35 + return new AsyncResult<>(insert == 1);
26 } 36 }
  37 +
27 } 38 }
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/package-info.java 0 → 100644
  1 +/**
  2 + * system 包下,我们放通用业务,支撑上层的核心业务。
  3 + * 例如说:用户、部门、权限、数据字典等等
  4 + *
  5 + * 缩写:sys
  6 + */
  7 +package cn.iocoder.yudao.userserver.modules.system;