- 后端:增加异常日志的记录
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package cn.iocoder.mall.admin.convert;
|
||||
|
||||
import cn.iocoder.mall.admin.api.dto.AccessLogAddDTO;
|
||||
import cn.iocoder.mall.admin.api.dto.ExceptionLogAddDTO;
|
||||
import cn.iocoder.mall.admin.dataobject.AccessLogDO;
|
||||
import cn.iocoder.mall.admin.dataobject.ExceptionLogDO;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mappings;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
@@ -14,4 +16,7 @@ public interface AccessLogConvert {
|
||||
@Mappings({})
|
||||
AccessLogDO convert(AccessLogAddDTO accessLogAddDTO);
|
||||
|
||||
@Mappings({})
|
||||
ExceptionLogDO convert(ExceptionLogAddDTO exceptionLogAddDTO);
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package cn.iocoder.mall.admin.dao;
|
||||
|
||||
import cn.iocoder.mall.admin.dataobject.ExceptionLogDO;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface ExceptionLogMapper extends BaseMapper<ExceptionLogDO> {
|
||||
}
|
||||
@@ -1,18 +1,20 @@
|
||||
package cn.iocoder.mall.admin.dataobject;
|
||||
|
||||
import cn.iocoder.common.framework.dataobject.DeletableDO;
|
||||
import cn.iocoder.common.framework.dataobject.BaseDO;
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 管理员访问日志 DO
|
||||
* 访问日志 DO
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class AccessLogDO extends DeletableDO {
|
||||
@TableName("access_log")
|
||||
public class AccessLogDO extends BaseDO {
|
||||
|
||||
/**
|
||||
* 编号
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
package cn.iocoder.mall.admin.dataobject;
|
||||
|
||||
import cn.iocoder.common.framework.dataobject.BaseDO;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 异常日志 DO
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
@TableName("exception_log")
|
||||
public class ExceptionLogDO extends BaseDO {
|
||||
|
||||
/**
|
||||
* 编号
|
||||
*/
|
||||
private Integer id;
|
||||
/**
|
||||
* 链路追踪编号
|
||||
*
|
||||
* 一般来说,通过链路追踪编号,可以将访问日志,错误日志,链路追踪日志,logger 打印日志等,结合在一起,从而进行排错。
|
||||
*/
|
||||
private String traceId;
|
||||
/**
|
||||
* 用户编号.
|
||||
*
|
||||
* 当管理员为空时,该值为 {@link cn.iocoder.mall.admin.api.dto.AccessLogAddDTO#USER_ID_NULL}
|
||||
*/
|
||||
private Integer userId;
|
||||
/**
|
||||
* 用户类型
|
||||
*/
|
||||
private Integer userType;
|
||||
/**
|
||||
* 应用名
|
||||
*
|
||||
* 目前读取 spring.application.name
|
||||
*/
|
||||
private String applicationName;
|
||||
/**
|
||||
* 访问地址
|
||||
*/
|
||||
private String uri;
|
||||
/**
|
||||
* 参数
|
||||
*/
|
||||
private String queryString;
|
||||
/**
|
||||
* http 方法
|
||||
*/
|
||||
private String method;
|
||||
/**
|
||||
* userAgent
|
||||
*/
|
||||
private String userAgent;
|
||||
/**
|
||||
* ip
|
||||
*/
|
||||
private String ip;
|
||||
/**
|
||||
* 异常发生时间
|
||||
*/
|
||||
private Date exceptionTime;
|
||||
/**
|
||||
* 异常名
|
||||
*
|
||||
* {@link Throwable#getClass()} 的类全名
|
||||
*/
|
||||
private String exceptionName;
|
||||
/**
|
||||
* 异常导致的消息
|
||||
*
|
||||
* {@link cn.iocoder.common.framework.util.ExceptionUtil#getMessage(Throwable)}
|
||||
*/
|
||||
private String exceptionMessage;
|
||||
/**
|
||||
* 异常导致的根消息
|
||||
*
|
||||
* {@link cn.iocoder.common.framework.util.ExceptionUtil#getRootCauseMessage(Throwable)}
|
||||
*/
|
||||
private String exceptionRootCauseMessage;
|
||||
/**
|
||||
* 异常的栈轨迹
|
||||
*
|
||||
* {@link cn.iocoder.common.framework.util.ExceptionUtil#getServiceException(Exception)}
|
||||
*/
|
||||
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;
|
||||
|
||||
}
|
||||
@@ -32,6 +32,9 @@ public class DataDictServiceImpl implements DataDictService {
|
||||
|
||||
@Override
|
||||
public CommonResult<List<DataDictBO>> selectDataDictList() {
|
||||
if (true) {
|
||||
throw new NullPointerException("阿拉啦啦啦");
|
||||
}
|
||||
List<DataDictDO> dataDicts = dataDictMapper.selectList();
|
||||
return CommonResult.success(DataDictConvert.INSTANCE.convert(dataDicts));
|
||||
}
|
||||
|
||||
@@ -3,9 +3,12 @@ package cn.iocoder.mall.admin.service;
|
||||
import cn.iocoder.common.framework.util.StringUtil;
|
||||
import cn.iocoder.mall.admin.api.SystemLogService;
|
||||
import cn.iocoder.mall.admin.api.dto.AccessLogAddDTO;
|
||||
import cn.iocoder.mall.admin.api.dto.ExceptionLogAddDTO;
|
||||
import cn.iocoder.mall.admin.convert.AccessLogConvert;
|
||||
import cn.iocoder.mall.admin.dao.AccessLogMapper;
|
||||
import cn.iocoder.mall.admin.dao.ExceptionLogMapper;
|
||||
import cn.iocoder.mall.admin.dataobject.AccessLogDO;
|
||||
import cn.iocoder.mall.admin.dataobject.ExceptionLogDO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -30,8 +33,11 @@ public class SystemLogServiceImpl implements SystemLogService {
|
||||
|
||||
@Autowired
|
||||
private AccessLogMapper accessLogMapper;
|
||||
@Autowired
|
||||
private ExceptionLogMapper exceptionLogMapper;
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("Duplicates")
|
||||
public void addAccessLog(AccessLogAddDTO adminAccessLogAddDTO) {
|
||||
// 创建 AdminAccessLogDO
|
||||
AccessLogDO accessLog = AccessLogConvert.INSTANCE.convert(adminAccessLogAddDTO);
|
||||
@@ -50,4 +56,24 @@ public class SystemLogServiceImpl implements SystemLogService {
|
||||
accessLogMapper.insert(accessLog);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("Duplicates")
|
||||
public void addExceptionLog(ExceptionLogAddDTO exceptionLogAddDTO) {
|
||||
// 创建 AdminAccessLogDO
|
||||
ExceptionLogDO exceptionLog = AccessLogConvert.INSTANCE.convert(exceptionLogAddDTO);
|
||||
exceptionLog.setCreateTime(new Date());
|
||||
// 截取最大长度
|
||||
if (exceptionLog.getUri().length() > URI_MAX_LENGTH) {
|
||||
exceptionLog.setUri(StringUtil.substring(exceptionLog.getUri(), URI_MAX_LENGTH));
|
||||
}
|
||||
if (exceptionLog.getQueryString().length() > QUERY_STRING_MAX_LENGTH) {
|
||||
exceptionLog.setQueryString(StringUtil.substring(exceptionLog.getQueryString(), QUERY_STRING_MAX_LENGTH));
|
||||
}
|
||||
if (exceptionLog.getUserAgent().length() > USER_AGENT_MAX_LENGTH) {
|
||||
exceptionLog.setUserAgent(StringUtil.substring(exceptionLog.getUserAgent(), USER_AGENT_MAX_LENGTH));
|
||||
}
|
||||
// 插入
|
||||
exceptionLogMapper.insert(exceptionLog);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,14 +7,14 @@
|
||||
<!--create_time-->
|
||||
<!--</sql>-->
|
||||
|
||||
<insert id="insert" parameterType="AccessLogDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
|
||||
INSERT INTO access_log (
|
||||
trace_id, user_id, user_type, uri, query_string, method, user_agent,
|
||||
ip, start_time, response_time, error_code, error_message, create_time
|
||||
) VALUES (
|
||||
#{traceId}, #{userId}, #{userType}, #{uri}, #{queryString}, #{method}, #{userAgent},
|
||||
#{ip}, #{startTime}, #{responseTime}, #{errorCode}, #{errorMessage}, #{createTime}
|
||||
)
|
||||
</insert>
|
||||
<!-- <insert id="insert" parameterType="AccessLogDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">-->
|
||||
<!-- INSERT INTO access_log (-->
|
||||
<!-- trace_id, user_id, user_type, uri, query_string, method, user_agent,-->
|
||||
<!-- ip, start_time, response_time, error_code, error_message, create_time-->
|
||||
<!-- ) VALUES (-->
|
||||
<!-- #{traceId}, #{userId}, #{userType}, #{uri}, #{queryString}, #{method}, #{userAgent},-->
|
||||
<!-- #{ip}, #{startTime}, #{responseTime}, #{errorCode}, #{errorMessage}, #{createTime}-->
|
||||
<!-- )-->
|
||||
<!-- </insert>-->
|
||||
|
||||
</mapper>
|
||||
|
||||
Reference in New Issue
Block a user