From 15e2c0945d448c72c78ed59911f898a96abe74f8 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Mon, 27 Sep 2021 09:12:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E8=AE=BF=E9=97=AE?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E3=80=81=E5=BC=82=E5=B8=B8=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E7=9A=84=E6=8F=92=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/InfApiAccessLogServiceImpl.java | 3 +- .../logger/InfApiAccessLogConvert.java | 20 +++ .../convert/logger/InfApiErrorLogConvert.java | 20 +++ .../modules/infra/convert/package-info.java | 6 + ...道 Spring Boot 对象转换 MapStruct 入门》.md | 1 + .../dataobject/logger/InfApiAccessLogDO.java | 107 ++++++++++++ .../dataobject/logger/InfApiErrorLogDO.java | 152 ++++++++++++++++++ .../mysql/logger/InfApiAccessLogMapper.java | 16 ++ .../mysql/logger/InfApiErrorLogMapper.java | 14 ++ .../InfApiErrorLogProcessStatusEnum.java | 28 ++++ .../modules/infra/enums/package-info.java | 4 + .../impl/InfApiAccessLogServiceImpl.java | 13 +- .../impl/InfApiErrorLogServiceImpl.java | 15 +- .../modules/system/package-info.java | 7 + 14 files changed, 400 insertions(+), 6 deletions(-) create mode 100644 yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/convert/logger/InfApiAccessLogConvert.java create mode 100644 yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/convert/logger/InfApiErrorLogConvert.java create mode 100644 yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/convert/package-info.java create mode 100644 yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/convert/《芋道 Spring Boot 对象转换 MapStruct 入门》.md create mode 100644 yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/dal/dataobject/logger/InfApiAccessLogDO.java create mode 100644 yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/dal/dataobject/logger/InfApiErrorLogDO.java create mode 100644 yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/dal/mysql/logger/InfApiAccessLogMapper.java create mode 100644 yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/dal/mysql/logger/InfApiErrorLogMapper.java create mode 100644 yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/enums/logger/InfApiErrorLogProcessStatusEnum.java create mode 100644 yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/enums/package-info.java create mode 100644 yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/package-info.java diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/infra/service/logger/impl/InfApiAccessLogServiceImpl.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/infra/service/logger/impl/InfApiAccessLogServiceImpl.java index 8fae484ac..aba6382d2 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/infra/service/logger/impl/InfApiAccessLogServiceImpl.java +++ b/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 { @Override @Async public Future createApiAccessLogAsync(ApiAccessLogCreateDTO createDTO) { - // 插入 InfApiAccessLogDO apiAccessLog = InfApiAccessLogConvert.INSTANCE.convert(createDTO); int insert = apiAccessLogMapper.insert(apiAccessLog); - return new AsyncResult<>(insert == 1); + return new AsyncResult<>(insert > 1); } @Override diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/convert/logger/InfApiAccessLogConvert.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/convert/logger/InfApiAccessLogConvert.java new file mode 100644 index 000000000..3ddbbf65b --- /dev/null +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/convert/logger/InfApiAccessLogConvert.java @@ -0,0 +1,20 @@ +package cn.iocoder.yudao.userserver.modules.infra.convert.logger; + +import cn.iocoder.yudao.framework.apilog.core.service.dto.ApiAccessLogCreateDTO; +import cn.iocoder.yudao.userserver.modules.infra.dal.dataobject.logger.InfApiAccessLogDO; +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +/** + * API 访问日志 Convert + * + * @author 芋道源码 + */ +@Mapper +public interface InfApiAccessLogConvert { + + InfApiAccessLogConvert INSTANCE = Mappers.getMapper(InfApiAccessLogConvert.class); + + InfApiAccessLogDO convert(ApiAccessLogCreateDTO bean); + +} diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/convert/logger/InfApiErrorLogConvert.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/convert/logger/InfApiErrorLogConvert.java new file mode 100644 index 000000000..51c6c3eef --- /dev/null +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/convert/logger/InfApiErrorLogConvert.java @@ -0,0 +1,20 @@ +package cn.iocoder.yudao.userserver.modules.infra.convert.logger; + +import cn.iocoder.yudao.framework.apilog.core.service.dto.ApiErrorLogCreateDTO; +import cn.iocoder.yudao.userserver.modules.infra.dal.dataobject.logger.InfApiErrorLogDO; +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +/** + * API 错误日志 Convert + * + * @author 芋道源码 + */ +@Mapper +public interface InfApiErrorLogConvert { + + InfApiErrorLogConvert INSTANCE = Mappers.getMapper(InfApiErrorLogConvert.class); + + InfApiErrorLogDO convert(ApiErrorLogCreateDTO bean); + +} diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/convert/package-info.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/convert/package-info.java new file mode 100644 index 000000000..d1aa5643a --- /dev/null +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/convert/package-info.java @@ -0,0 +1,6 @@ +/** + * 提供 POJO 类的实体转换 + * + * 目前使用 MapStruct 框架 + */ +package cn.iocoder.yudao.userserver.modules.infra.convert; diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/convert/《芋道 Spring Boot 对象转换 MapStruct 入门》.md b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/convert/《芋道 Spring Boot 对象转换 MapStruct 入门》.md new file mode 100644 index 000000000..8153487b7 --- /dev/null +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/convert/《芋道 Spring Boot 对象转换 MapStruct 入门》.md @@ -0,0 +1 @@ + diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/dal/dataobject/logger/InfApiAccessLogDO.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/dal/dataobject/logger/InfApiAccessLogDO.java new file mode 100644 index 000000000..2548ec469 --- /dev/null +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/dal/dataobject/logger/InfApiAccessLogDO.java @@ -0,0 +1,107 @@ +package cn.iocoder.yudao.userserver.modules.infra.dal.dataobject.logger; + +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; +import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.*; + +import java.util.Date; + +/** + * API 访问日志 + * + * @author 芋道源码 + */ +@TableName("inf_api_access_log") +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class InfApiAccessLogDO extends BaseDO { + + /** + * 编号 + */ + @TableId + private Integer id; + /** + * 链路追踪编号 + * + * 一般来说,通过链路追踪编号,可以将访问日志,错误日志,链路追踪日志,logger 打印日志等,结合在一起,从而进行排错。 + */ + private String traceId; + /** + * 用户编号 + */ + private Long userId; + /** + * 用户类型 + * + * 枚举 {@link UserTypeEnum} + */ + private Integer userType; + /** + * 应用名 + * + * 目前读取 `spring.application.name` 配置项 + */ + private String applicationName; + + // ========== 请求相关字段 ========== + + /** + * 请求方法名 + */ + private String requestMethod; + /** + * 访问地址 + */ + private String requestUrl; + /** + * 请求参数 + * + * query: Query String + * body: Quest Body + */ + private String requestParams; + /** + * 用户 IP + */ + private String userIp; + /** + * 浏览器 UA + */ + private String userAgent; + + // ========== 执行相关字段 ========== + + /** + * 开始请求时间 + */ + private Date beginTime; + /** + * 结束请求时间 + */ + private Date endTime; + /** + * 执行时长,单位:毫秒 + */ + private Integer duration; + /** + * 结果码 + * + * 目前使用的 {@link CommonResult#getCode()} 属性 + */ + private Integer resultCode; + /** + * 结果提示 + * + * 目前使用的 {@link CommonResult#getMsg()} 属性 + */ + private String resultMsg; + +} diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/dal/dataobject/logger/InfApiErrorLogDO.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/dal/dataobject/logger/InfApiErrorLogDO.java new file mode 100644 index 000000000..6ed2b3150 --- /dev/null +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/dal/dataobject/logger/InfApiErrorLogDO.java @@ -0,0 +1,152 @@ +package cn.iocoder.yudao.userserver.modules.infra.dal.dataobject.logger; + +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; +import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; +import cn.iocoder.yudao.userserver.modules.infra.enums.logger.InfApiErrorLogProcessStatusEnum; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.*; + +import java.util.Date; + +/** + * API 异常数据 + * + * @author 芋道源码 + */ +@TableName("inf_api_error_log") +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class InfApiErrorLogDO extends BaseDO { + + /** + * 编号 + */ + private Long id; + /** + * 用户编号 + */ + private Long userId; + /** + * 链路追踪编号 + * + * 一般来说,通过链路追踪编号,可以将访问日志,错误日志,链路追踪日志,logger 打印日志等,结合在一起,从而进行排错。 + */ + private String traceId; + /** + * 用户类型 + * + * 枚举 {@link UserTypeEnum} + */ + private Integer userType; + /** + * 应用名 + * + * 目前读取 spring.application.name + */ + private String applicationName; + + // ========== 请求相关字段 ========== + + /** + * 请求方法名 + */ + private String requestMethod; + /** + * 访问地址 + */ + private String requestUrl; + /** + * 请求参数 + * + * query: Query String + * body: Quest Body + */ + private String requestParams; + /** + * 用户 IP + */ + private String userIp; + /** + * 浏览器 UA + */ + private String userAgent; + + // ========== 异常相关字段 ========== + + /** + * 异常发生时间 + */ + private Date exceptionTime; + /** + * 异常名 + * + * {@link Throwable#getClass()} 的类全名 + */ + private String exceptionName; + /** + * 异常导致的消息 + * + * {@link cn.hutool.core.exceptions.ExceptionUtil#getMessage(Throwable)} + */ + private String exceptionMessage; + /** + * 异常导致的根消息 + * + * {@link cn.hutool.core.exceptions.ExceptionUtil#getRootCauseMessage(Throwable)} + */ + private String exceptionRootCauseMessage; + /** + * 异常的栈轨迹 + * + * {@link org.apache.commons.lang3.exception.ExceptionUtils#getStackTrace(Throwable)} + */ + private String exceptionStackTrace; + /** + * 异常发生的类全名 + * + * {@link StackTraceElement#getClassName()} + */ + private String exceptionClassName; + /** + * 异常发生的类文件 + * + * {@link StackTraceElement#getFileName()} + */ + private String exceptionFileName; + /** + * 异常发生的方法名 + * + * {@link StackTraceElement#getMethodName()} + */ + private String exceptionMethodName; + /** + * 异常发生的方法所在行 + * + * {@link StackTraceElement#getLineNumber()} + */ + private Integer exceptionLineNumber; + + // ========== 处理相关字段 ========== + + /** + * 处理状态 + * + * 枚举 {@link InfApiErrorLogProcessStatusEnum} + */ + private Integer processStatus; + /** + * 处理时间 + */ + private Date processTime; + /** + * 处理用户编号 + * + * 关联 cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.user.SysUserDO.SysUserDO#getId() + */ + private Long processUserId; + +} diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/dal/mysql/logger/InfApiAccessLogMapper.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/dal/mysql/logger/InfApiAccessLogMapper.java new file mode 100644 index 000000000..cf7f6e92c --- /dev/null +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/dal/mysql/logger/InfApiAccessLogMapper.java @@ -0,0 +1,16 @@ +package cn.iocoder.yudao.userserver.modules.infra.dal.mysql.logger; + +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; +import cn.iocoder.yudao.userserver.modules.infra.dal.dataobject.logger.InfApiAccessLogDO; +import org.apache.ibatis.annotations.Mapper; + +import java.util.List; + +/** + * API 访问日志 Mapper + * + * @author 芋道源码 + */ +@Mapper +public interface InfApiAccessLogMapper extends BaseMapperX { +} diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/dal/mysql/logger/InfApiErrorLogMapper.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/dal/mysql/logger/InfApiErrorLogMapper.java new file mode 100644 index 000000000..9e9124267 --- /dev/null +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/dal/mysql/logger/InfApiErrorLogMapper.java @@ -0,0 +1,14 @@ +package cn.iocoder.yudao.userserver.modules.infra.dal.mysql.logger; + +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; +import cn.iocoder.yudao.userserver.modules.infra.dal.dataobject.logger.InfApiErrorLogDO; +import org.apache.ibatis.annotations.Mapper; + +/** + * API 错误日志 Mapper + * + * @author 芋道源码 + */ +@Mapper +public interface InfApiErrorLogMapper extends BaseMapperX { +} diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/enums/logger/InfApiErrorLogProcessStatusEnum.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/enums/logger/InfApiErrorLogProcessStatusEnum.java new file mode 100644 index 000000000..618d5d9be --- /dev/null +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/enums/logger/InfApiErrorLogProcessStatusEnum.java @@ -0,0 +1,28 @@ +package cn.iocoder.yudao.userserver.modules.infra.enums.logger; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * API 异常数据的处理状态 + * + * @author 芋道源码 + */ +@AllArgsConstructor +@Getter +public enum InfApiErrorLogProcessStatusEnum { + + INIT(0, "未处理"), + DONE(1, "已处理"), + IGNORE(2, "已忽略"); + + /** + * 状态 + */ + private final Integer status; + /** + * 资源类型名 + */ + private final String name; + +} diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/enums/package-info.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/enums/package-info.java new file mode 100644 index 000000000..3a03078cb --- /dev/null +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/enums/package-info.java @@ -0,0 +1,4 @@ +/** + * 占位类,可以无视 + */ +package cn.iocoder.yudao.userserver.modules.infra.enums; diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/service/logger/impl/InfApiAccessLogServiceImpl.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/service/logger/impl/InfApiAccessLogServiceImpl.java index c246d1cfb..1b0f2611c 100644 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/service/logger/impl/InfApiAccessLogServiceImpl.java +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/service/logger/impl/InfApiAccessLogServiceImpl.java @@ -1,12 +1,16 @@ package cn.iocoder.yudao.userserver.modules.infra.service.logger.impl; import cn.iocoder.yudao.framework.apilog.core.service.dto.ApiAccessLogCreateDTO; +import cn.iocoder.yudao.userserver.modules.infra.convert.logger.InfApiAccessLogConvert; +import cn.iocoder.yudao.userserver.modules.infra.dal.dataobject.logger.InfApiAccessLogDO; +import cn.iocoder.yudao.userserver.modules.infra.dal.mysql.logger.InfApiAccessLogMapper; import cn.iocoder.yudao.userserver.modules.infra.service.logger.InfApiAccessLogService; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.AsyncResult; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; +import javax.annotation.Resource; import java.util.concurrent.Future; /** @@ -19,9 +23,14 @@ import java.util.concurrent.Future; @Slf4j public class InfApiAccessLogServiceImpl implements InfApiAccessLogService { + @Resource + private InfApiAccessLogMapper apiAccessLogMapper; + @Override public Future createApiAccessLogAsync(ApiAccessLogCreateDTO createDTO) { - log.debug("{}", createDTO); - return new AsyncResult<>(true); + InfApiAccessLogDO apiAccessLog = InfApiAccessLogConvert.INSTANCE.convert(createDTO); + int insert = apiAccessLogMapper.insert(apiAccessLog); + return new AsyncResult<>(insert > 1); } + } diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/service/logger/impl/InfApiErrorLogServiceImpl.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/service/logger/impl/InfApiErrorLogServiceImpl.java index 637cb63ff..05bddb7d9 100644 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/service/logger/impl/InfApiErrorLogServiceImpl.java +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/service/logger/impl/InfApiErrorLogServiceImpl.java @@ -1,12 +1,17 @@ package cn.iocoder.yudao.userserver.modules.infra.service.logger.impl; import cn.iocoder.yudao.framework.apilog.core.service.dto.ApiErrorLogCreateDTO; +import cn.iocoder.yudao.userserver.modules.infra.convert.logger.InfApiErrorLogConvert; +import cn.iocoder.yudao.userserver.modules.infra.dal.dataobject.logger.InfApiErrorLogDO; +import cn.iocoder.yudao.userserver.modules.infra.dal.mysql.logger.InfApiErrorLogMapper; +import cn.iocoder.yudao.userserver.modules.infra.enums.logger.InfApiErrorLogProcessStatusEnum; import cn.iocoder.yudao.userserver.modules.infra.service.logger.InfApiErrorLogService; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.AsyncResult; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; +import javax.annotation.Resource; import java.util.concurrent.Future; /** @@ -19,9 +24,15 @@ import java.util.concurrent.Future; @Slf4j public class InfApiErrorLogServiceImpl implements InfApiErrorLogService { + @Resource + private InfApiErrorLogMapper apiErrorLogMapper; + @Override public Future createApiErrorLogAsync(ApiErrorLogCreateDTO createDTO) { - log.debug("{}", createDTO); - return new AsyncResult<>(true); + InfApiErrorLogDO apiErrorLog = InfApiErrorLogConvert.INSTANCE.convert(createDTO); + apiErrorLog.setProcessStatus(InfApiErrorLogProcessStatusEnum.INIT.getStatus()); + int insert = apiErrorLogMapper.insert(apiErrorLog); + return new AsyncResult<>(insert == 1); } + } diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/package-info.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/package-info.java new file mode 100644 index 000000000..89fa9c162 --- /dev/null +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/package-info.java @@ -0,0 +1,7 @@ +/** + * system 包下,我们放通用业务,支撑上层的核心业务。 + * 例如说:用户、部门、权限、数据字典等等 + * + * 缩写:sys + */ +package cn.iocoder.yudao.userserver.modules.system;