将 login_log、error_log 迁移到 yudao-core-service 项目中

This commit is contained in:
YunaiV
2021-10-10 18:49:39 +08:00
parent 028c99aa10
commit d784b113af
83 changed files with 432 additions and 748 deletions

View File

@ -0,0 +1,15 @@
package cn.iocoder.yudao.coreservice.modules.infra.convert.logger;
import cn.iocoder.yudao.framework.apilog.core.service.dto.ApiAccessLogCreateDTO;
import cn.iocoder.yudao.coreservice.modules.infra.dal.dataobject.logger.InfApiAccessLogDO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@Mapper
public interface InfApiAccessLogCoreConvert {
InfApiAccessLogCoreConvert INSTANCE = Mappers.getMapper(InfApiAccessLogCoreConvert.class);
InfApiAccessLogDO convert(ApiAccessLogCreateDTO bean);
}

View File

@ -0,0 +1,15 @@
package cn.iocoder.yudao.coreservice.modules.infra.convert.logger;
import cn.iocoder.yudao.framework.apilog.core.service.dto.ApiErrorLogCreateDTO;
import cn.iocoder.yudao.coreservice.modules.infra.dal.dataobject.logger.InfApiErrorLogDO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@Mapper
public interface InfApiErrorLogCoreConvert {
InfApiErrorLogCoreConvert INSTANCE = Mappers.getMapper(InfApiErrorLogCoreConvert.class);
InfApiErrorLogDO convert(ApiErrorLogCreateDTO bean);
}

View File

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

View File

@ -0,0 +1,64 @@
package cn.iocoder.yudao.coreservice.modules.infra.dal.dataobject.config;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
/**
* 参数配置表
*
* @author ruoyi
*/
@TableName("inf_config")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class InfConfigDO extends BaseDO {
/**
* 参数主键
*/
@TableId
private Long id;
/**
* 参数分组
*/
@TableField("`group`")
private String group;
/**
* 参数名称
*/
private String name;
/**
* 参数键名
*/
@TableField("`key`")
private String key;
/**
* 参数键值
*/
private String value;
/**
* 参数类型
*
* 枚举 {@link InfConfigTypeEnum}
*/
@TableField("`type`")
private Integer type;
/**
* 是否敏感
*
* 对于敏感配置,需要管理权限才能查看
*/
@TableField("`sensitive`")
private Boolean sensitive;
/**
* 备注
*/
private String remark;
}

View File

@ -0,0 +1,43 @@
package cn.iocoder.yudao.coreservice.modules.infra.dal.dataobject.file;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
import java.io.InputStream;
/**
* 文件表
*
* @author 芋道源码
*/
@Data
@TableName("inf_file")
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class InfFileDO extends BaseDO {
/**
* 文件路径
*/
@TableId(type = IdType.INPUT)
private String id;
/**
* 文件类型
*
* 通过 {@link cn.hutool.core.io.FileTypeUtil#getType(InputStream)} 获取
*/
@TableField(value = "`type`")
private String type;
/**
* 文件内容
*/
private byte[] content;
}

View File

@ -0,0 +1,107 @@
package cn.iocoder.yudao.coreservice.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.coreservice.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.coreservice.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,39 @@
package cn.iocoder.yudao.coreservice.modules.infra.dal.mysql.config;
import cn.iocoder.yudao.framework.apollo.internals.ConfigFrameworkDAO;
import cn.iocoder.yudao.framework.apollo.internals.dto.ConfigRespDTO;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.util.Date;
import java.util.List;
/**
* ConfigFrameworkDAO Core 实现类
*
* @author 芋道源码
*/
public class InfConfigCoreDAOImpl implements ConfigFrameworkDAO {
private final JdbcTemplate jdbcTemplate;
public InfConfigCoreDAOImpl(String jdbcUrl, String username, String password) {
DataSource dataSource = new DriverManagerDataSource(jdbcUrl, username, password);
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
@Override
public boolean selectExistsByUpdateTimeAfter(Date maxUpdateTime) {
return jdbcTemplate.query("SELECT id FROM inf_config WHERE update_time > ? LIMIT 1",
ResultSet::next, maxUpdateTime);
}
@Override
public List<ConfigRespDTO> selectList() {
return jdbcTemplate.query("SELECT `key`, `value`, update_time, deleted FROM inf_config", new BeanPropertyRowMapper<>(ConfigRespDTO.class));
}
}

View File

@ -0,0 +1,10 @@
package cn.iocoder.yudao.coreservice.modules.infra.dal.mysql.file;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.coreservice.modules.infra.dal.dataobject.file.InfFileDO;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface InfFileCoreMapper extends BaseMapperX<InfFileDO> {
}

View File

@ -0,0 +1,14 @@
package cn.iocoder.yudao.coreservice.modules.infra.dal.mysql.logger;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.coreservice.modules.infra.dal.dataobject.logger.InfApiAccessLogDO;
import org.apache.ibatis.annotations.Mapper;
/**
* API 访问日志 Mapper
*
* @author 芋道源码
*/
@Mapper
public interface InfApiAccessLogCoreMapper extends BaseMapperX<InfApiAccessLogDO> {
}

View File

@ -0,0 +1,9 @@
package cn.iocoder.yudao.coreservice.modules.infra.dal.mysql.logger;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.coreservice.modules.infra.dal.dataobject.logger.InfApiErrorLogDO;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface InfApiErrorLogCoreMapper extends BaseMapperX<InfApiErrorLogDO> {
}

View File

@ -0,0 +1,28 @@
package cn.iocoder.yudao.coreservice.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.coreservice.modules.infra.enums;

View File

@ -0,0 +1,12 @@
package cn.iocoder.yudao.coreservice.modules.infra.service.logger;
import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLogFrameworkService;
/**
* API 访问日志 Service 接口
*
* @author 芋道源码
*/
public interface InfApiAccessLogCoreService extends ApiAccessLogFrameworkService {
}

View File

@ -0,0 +1,12 @@
package cn.iocoder.yudao.coreservice.modules.infra.service.logger;
import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkService;
/**
* API 错误日志 Service 接口
*
* @author 芋道源码
*/
public interface InfApiErrorLogCoreService extends ApiErrorLogFrameworkService {
}

View File

@ -0,0 +1,37 @@
package cn.iocoder.yudao.coreservice.modules.infra.service.logger.impl;
import cn.iocoder.yudao.coreservice.modules.infra.convert.logger.InfApiAccessLogCoreConvert;
import cn.iocoder.yudao.framework.apilog.core.service.dto.ApiAccessLogCreateDTO;
import cn.iocoder.yudao.coreservice.modules.infra.dal.dataobject.logger.InfApiAccessLogDO;
import cn.iocoder.yudao.coreservice.modules.infra.dal.mysql.logger.InfApiAccessLogCoreMapper;
import cn.iocoder.yudao.coreservice.modules.infra.service.logger.InfApiAccessLogCoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
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;
/**
* API 访问日志 Service 实现类
*
* @author 芋道源码
*/
@Service
@Validated
@Slf4j
public class InfApiAccessLogCoreServiceImpl implements InfApiAccessLogCoreService {
@Resource
private InfApiAccessLogCoreMapper apiAccessLogMapper;
@Override
@Async
public void createApiAccessLogAsync(ApiAccessLogCreateDTO createDTO) {
InfApiAccessLogDO apiAccessLog = InfApiAccessLogCoreConvert.INSTANCE.convert(createDTO);
apiAccessLogMapper.insert(apiAccessLog);
}
}

View File

@ -0,0 +1,39 @@
package cn.iocoder.yudao.coreservice.modules.infra.service.logger.impl;
import cn.iocoder.yudao.coreservice.modules.infra.convert.logger.InfApiErrorLogCoreConvert;
import cn.iocoder.yudao.coreservice.modules.infra.dal.dataobject.logger.InfApiErrorLogDO;
import cn.iocoder.yudao.coreservice.modules.infra.dal.mysql.logger.InfApiErrorLogCoreMapper;
import cn.iocoder.yudao.coreservice.modules.infra.enums.logger.InfApiErrorLogProcessStatusEnum;
import cn.iocoder.yudao.coreservice.modules.infra.service.logger.InfApiErrorLogCoreService;
import cn.iocoder.yudao.framework.apilog.core.service.dto.ApiErrorLogCreateDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
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;
/**
* API 错误日志 Service 实现类
*
* @author 芋道源码
*/
@Service
@Validated
@Slf4j
public class InfApiErrorLogCoreServiceImpl implements InfApiErrorLogCoreService {
@Resource
private InfApiErrorLogCoreMapper apiErrorLogMapper;
@Override
@Async
public void createApiErrorLogAsync(ApiErrorLogCreateDTO createDTO) {
InfApiErrorLogDO apiErrorLog = InfApiErrorLogCoreConvert.INSTANCE.convert(createDTO);
apiErrorLog.setProcessStatus(InfApiErrorLogProcessStatusEnum.INIT.getStatus());
apiErrorLogMapper.insert(apiErrorLog);
}
}

View File

@ -0,0 +1,46 @@
package cn.iocoder.yudao.coreservice.modules.infra.service.logger;
import cn.hutool.core.util.RandomUtil;
import cn.iocoder.yudao.coreservice.BaseDbUnitTest;
import cn.iocoder.yudao.coreservice.modules.infra.dal.dataobject.logger.InfApiAccessLogDO;
import cn.iocoder.yudao.coreservice.modules.infra.dal.mysql.logger.InfApiAccessLogCoreMapper;
import cn.iocoder.yudao.coreservice.modules.infra.service.logger.impl.InfApiAccessLogCoreServiceImpl;
import cn.iocoder.yudao.framework.apilog.core.service.dto.ApiAccessLogCreateDTO;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.test.core.util.RandomUtils;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Import;
import javax.annotation.Resource;
import java.util.concurrent.Future;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
/**
* {@link InfApiAccessLogCoreServiceImpl} 单元测试
*/
@Import(InfApiAccessLogCoreServiceImpl.class)
public class InfApiAccessLogCoreServiceTest extends BaseDbUnitTest {
@Resource
private InfApiAccessLogCoreService apiAccessLogCoreService;
@Resource
private InfApiAccessLogCoreMapper apiAccessLogCoreMapper;
@Test
public void testCreateApiAccessLogAsync() {
// 准备参数
ApiAccessLogCreateDTO createDTO = RandomUtils.randomPojo(ApiAccessLogCreateDTO.class,
dto -> dto.setUserType(RandomUtil.randomEle(UserTypeEnum.values()).getValue()));
// 调用
apiAccessLogCoreService.createApiAccessLogAsync(createDTO);
// 断言
InfApiAccessLogDO infApiAccessLogDO = apiAccessLogCoreMapper.selectOne(null);
assertNotNull(infApiAccessLogDO);
assertPojoEquals(createDTO, infApiAccessLogDO);
}
}

View File

@ -0,0 +1,45 @@
package cn.iocoder.yudao.coreservice.modules.infra.service.logger;
import cn.hutool.core.util.RandomUtil;
import cn.iocoder.yudao.coreservice.BaseDbUnitTest;
import cn.iocoder.yudao.coreservice.modules.infra.dal.dataobject.logger.InfApiErrorLogDO;
import cn.iocoder.yudao.coreservice.modules.infra.dal.mysql.logger.InfApiErrorLogCoreMapper;
import cn.iocoder.yudao.coreservice.modules.infra.service.logger.impl.InfApiErrorLogCoreServiceImpl;
import cn.iocoder.yudao.framework.apilog.core.service.dto.ApiErrorLogCreateDTO;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.test.core.util.RandomUtils;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Import;
import javax.annotation.Resource;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
/**
* {@link InfApiErrorLogCoreServiceImpl} 单元测试
*/
@Import(InfApiErrorLogCoreServiceImpl.class)
public class InfApiErrorLogCoreServiceTest extends BaseDbUnitTest {
@Resource
private InfApiErrorLogCoreService apiErrorLogCoreService;
@Resource
private InfApiErrorLogCoreMapper infApiErrorLogCoreMapper;
@Test
public void testCreateApiErrorLogAsync() {
// 准备参数
ApiErrorLogCreateDTO createDTO = RandomUtils.randomPojo(ApiErrorLogCreateDTO.class,
dto -> dto.setUserType(RandomUtil.randomEle(UserTypeEnum.values()).getValue()));
// 调用
apiErrorLogCoreService.createApiErrorLogAsync(createDTO);
// 断言
InfApiErrorLogDO infApiErrorLogDO = infApiErrorLogCoreMapper.selectOne(null);
assertNotNull(infApiErrorLogDO);
assertPojoEquals(createDTO, infApiErrorLogDO);
}
}

View File

@ -0,0 +1 @@
package cn.iocoder.yudao.coreservice.modules.infra.service;

View File

@ -1,4 +1,6 @@
-- inf 开头的 DB
DELETE FROM "inf_api_access_log";
DELETE FROM "inf_api_error_log";
-- sys 开头的 DB
DELETE FROM "sys_user_session";

View File

@ -33,3 +33,58 @@ CREATE TABLE IF NOT EXISTS "sys_dict_data" (
"deleted" bit NOT NULL DEFAULT FALSE,
PRIMARY KEY ("id")
) COMMENT '字典数据表';
CREATE TABLE IF NOT EXISTS "inf_api_access_log" (
"id" bigint not null GENERATED BY DEFAULT AS IDENTITY,
"trace_id" varchar(64) not null default '',
"user_id" bigint not null default '0',
"user_type" tinyint not null default '0',
"application_name" varchar(50) not null,
"request_method" varchar(16) not null default '',
"request_url" varchar(255) not null default '',
"request_params" varchar(8000) not null default '',
"user_ip" varchar(50) not null,
"user_agent" varchar(512) not null,
"begin_time" timestamp not null,
"end_time" timestamp not null,
"duration" integer not null,
"result_code" integer not null default '0',
"result_msg" varchar(512) default '',
"creator" varchar(64) default '',
"create_time" timestamp not null default current_timestamp,
"updater" varchar(64) default '',
"update_time" timestamp not null default current_timestamp,
"deleted" bit not null default false,
primary key ("id")
) COMMENT 'API 访问日志表';
CREATE TABLE IF NOT EXISTS "inf_api_error_log" (
"id" integer not null GENERATED BY DEFAULT AS IDENTITY,
"trace_id" varchar(64) not null,
"user_id" bigint not null default '0',
"user_type" tinyint not null default '0',
"application_name" varchar(50) not null,
"request_method" varchar(16) not null,
"request_url" varchar(255) not null,
"request_params" varchar(8000) not null,
"user_ip" varchar(50) not null,
"user_agent" varchar(512) not null,
"exception_time" timestamp not null,
"exception_name" varchar(128) not null default '',
"exception_message" clob not null,
"exception_root_cause_message" clob not null,
"exception_stack_trace" clob not null,
"exception_class_name" varchar(512) not null,
"exception_file_name" varchar(512) not null,
"exception_method_name" varchar(512) not null,
"exception_line_number" integer not null,
"process_status" tinyint not null,
"process_time" timestamp default null,
"process_user_id" bigint default '0',
"creator" varchar(64) default '',
"create_time" timestamp not null default current_timestamp,
"updater" varchar(64) default '',
"update_time" timestamp not null default current_timestamp,
"deleted" bit not null default false,
primary key ("id")
) COMMENT '系统异常日志';