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

This commit is contained in:
YunaiV 2021-09-27 09:12:43 +08:00
parent c81c275a97
commit 15e2c0945d
14 changed files with 400 additions and 6 deletions

View File

@ -32,10 +32,9 @@ public class InfApiAccessLogServiceImpl implements InfApiAccessLogService {
@Override @Override
@Async @Async
public Future<Boolean> createApiAccessLogAsync(ApiAccessLogCreateDTO createDTO) { public Future<Boolean> createApiAccessLogAsync(ApiAccessLogCreateDTO createDTO) {
// 插入
InfApiAccessLogDO apiAccessLog = InfApiAccessLogConvert.INSTANCE.convert(createDTO); InfApiAccessLogDO apiAccessLog = InfApiAccessLogConvert.INSTANCE.convert(createDTO);
int insert = apiAccessLogMapper.insert(apiAccessLog); int insert = apiAccessLogMapper.insert(apiAccessLog);
return new AsyncResult<>(insert == 1); return new AsyncResult<>(insert > 1);
} }
@Override @Override

View File

@ -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);
}

View File

@ -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);
}

View File

@ -0,0 +1,6 @@
/**
* 提供 POJO 类的实体转换
*
* 目前使用 MapStruct 框架
*/
package cn.iocoder.yudao.userserver.modules.infra.convert;

View File

@ -0,0 +1 @@
<http://www.iocoder.cn/Spring-Boot/MapStruct/?yudao>

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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> {
}

View File

@ -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> {
}

View File

@ -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;
}

View File

@ -0,0 +1,4 @@
/**
* 占位类可以无视
*/
package cn.iocoder.yudao.userserver.modules.infra.enums;

View File

@ -1,12 +1,16 @@
package cn.iocoder.yudao.userserver.modules.infra.service.logger.impl; package cn.iocoder.yudao.userserver.modules.infra.service.logger.impl;
import cn.iocoder.yudao.framework.apilog.core.service.dto.ApiAccessLogCreateDTO; 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 cn.iocoder.yudao.userserver.modules.infra.service.logger.InfApiAccessLogService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.AsyncResult; import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.util.concurrent.Future; import java.util.concurrent.Future;
/** /**
@ -19,9 +23,14 @@ import java.util.concurrent.Future;
@Slf4j @Slf4j
public class InfApiAccessLogServiceImpl implements InfApiAccessLogService { public class InfApiAccessLogServiceImpl implements InfApiAccessLogService {
@Resource
private InfApiAccessLogMapper apiAccessLogMapper;
@Override @Override
public Future<Boolean> createApiAccessLogAsync(ApiAccessLogCreateDTO createDTO) { public Future<Boolean> createApiAccessLogAsync(ApiAccessLogCreateDTO createDTO) {
log.debug("{}", createDTO); InfApiAccessLogDO apiAccessLog = InfApiAccessLogConvert.INSTANCE.convert(createDTO);
return new AsyncResult<>(true); int insert = apiAccessLogMapper.insert(apiAccessLog);
return new AsyncResult<>(insert > 1);
} }
} }

View File

@ -1,12 +1,17 @@
package cn.iocoder.yudao.userserver.modules.infra.service.logger.impl; package cn.iocoder.yudao.userserver.modules.infra.service.logger.impl;
import cn.iocoder.yudao.framework.apilog.core.service.dto.ApiErrorLogCreateDTO; 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 cn.iocoder.yudao.userserver.modules.infra.service.logger.InfApiErrorLogService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.AsyncResult; import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.util.concurrent.Future; import java.util.concurrent.Future;
/** /**
@ -19,9 +24,15 @@ import java.util.concurrent.Future;
@Slf4j @Slf4j
public class InfApiErrorLogServiceImpl implements InfApiErrorLogService { public class InfApiErrorLogServiceImpl implements InfApiErrorLogService {
@Resource
private InfApiErrorLogMapper apiErrorLogMapper;
@Override @Override
public Future<Boolean> createApiErrorLogAsync(ApiErrorLogCreateDTO createDTO) { public Future<Boolean> createApiErrorLogAsync(ApiErrorLogCreateDTO createDTO) {
log.debug("{}", createDTO); InfApiErrorLogDO apiErrorLog = InfApiErrorLogConvert.INSTANCE.convert(createDTO);
return new AsyncResult<>(true); apiErrorLog.setProcessStatus(InfApiErrorLogProcessStatusEnum.INIT.getStatus());
int insert = apiErrorLogMapper.insert(apiErrorLog);
return new AsyncResult<>(insert == 1);
} }
} }

View File

@ -0,0 +1,7 @@
/**
* system 包下我们放通用业务支撑上层的核心业务
* 例如说用户部门权限数据字典等等
*
* 缩写sys
*/
package cn.iocoder.yudao.userserver.modules.system;