mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-10-31 18:28:43 +08:00 
			
		
		
		
	初始化访问日志、异常日志的插入
This commit is contained in:
		| @@ -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); | ||||
|  | ||||
| } | ||||
| @@ -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); | ||||
|  | ||||
| } | ||||
| @@ -0,0 +1,6 @@ | ||||
| /** | ||||
|  * 提供 POJO 类的实体转换 | ||||
|  * | ||||
|  * 目前使用 MapStruct 框架 | ||||
|  */ | ||||
| package cn.iocoder.yudao.userserver.modules.infra.convert; | ||||
| @@ -0,0 +1 @@ | ||||
| <http://www.iocoder.cn/Spring-Boot/MapStruct/?yudao> | ||||
| @@ -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; | ||||
|  | ||||
| } | ||||
| @@ -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; | ||||
|  | ||||
| } | ||||
| @@ -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<InfApiAccessLogDO> { | ||||
| } | ||||
| @@ -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<InfApiErrorLogDO> { | ||||
| } | ||||
| @@ -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; | ||||
|  | ||||
| } | ||||
| @@ -0,0 +1,4 @@ | ||||
| /** | ||||
|  * 占位类,可以无视 | ||||
|  */ | ||||
| package cn.iocoder.yudao.userserver.modules.infra.enums; | ||||
| @@ -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<Boolean> 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); | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -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<Boolean> 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); | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -0,0 +1,7 @@ | ||||
| /** | ||||
|  * system 包下,我们放通用业务,支撑上层的核心业务。 | ||||
|  * 例如说:用户、部门、权限、数据字典等等 | ||||
|  * | ||||
|  * 缩写:sys | ||||
|  */ | ||||
| package cn.iocoder.yudao.userserver.modules.system; | ||||
		Reference in New Issue
	
	Block a user
	 YunaiV
					YunaiV