自己做的项目,平时去日志去看错误信息有点小麻烦,直接写入数据库,打开数据库就能看,方便一些,适合我这种bug比较多的。这个小工具自己用的,大家喜欢可以改善。------一个菜鸟的自述
#表结构
DROP TABLE IF EXISTS `exception_log`;
CREATE TABLE `exception_log` (
`id` varchar(19) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`create_time` datetime(0) NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '创建时间',
`user_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '用户',
`cause` varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '异常名字,取30行',
`message` varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '异常内容',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '异常信息' ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
实体创建
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* 异常信息
*
* @author unknown
* @email 1436199880@qq.com
* @date 2020-04-07 15:25:20
*/
@Data
@TableName("exception_log")
public class ExceptionLogEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* id
*/
@TableId
private String id;
/**
* 创建时间
*/
private Date createTime;
/**
*
*/
private String userId;
/**
* 异常名字
*/
private String cause;
/**
* 异常内容,取30行
*/
private String message;
}
Mapper
import com.baomidou.mybatisplus.mapper.BaseMapper;
import io.renren.modules.base.entity.ExceptionLogEntity;
import org.apache.ibatis.annotations.Mapper;
/**
* 错误日志处理
*
* @author liyongfei
* @email 1436199880@qq.com
* @date 2021-05-13 17:31:46
*/
@Mapper
public interface ExceptionLogMapper extends BaseMapper<ExceptionLogEntity> {
}
ExceptionHandler
import io.renren.common.utils.R;
import io.renren.modules.base.dao.ExceptionLogMapper;
import io.renren.modules.base.entity.ExceptionLogEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;
import javax.annotation.Resource;
/**
* 异常处理器
*
* @author
* @email
* @date 2016年10月27日 下午10:16:19
*/
@RestControllerAdvice
@Slf4j
public class RRExceptionHandler {
@Resource
private ExceptionLogMapper exceptionLogMapper;
/**
* 处理自定义异常
*/
@ExceptionHandler(RRException.class)
public R handleRRException(RRException e){
R r = new R();
r.put("code", e.getCode());
r.put("msg", e.getMessage());
ExceptionLogEntity exceptionLogEntity = handlerException(e);
exceptionLogMapper.insert(exceptionLogEntity);
return r;
}
@ExceptionHandler(NoHandlerFoundException.class)
public R handlerNoFoundException(Exception e) {
log.error(e.getMessage(), e);
return R.error(404, "路径不存在,请检查路径是否正确");
}
@ExceptionHandler(DuplicateKeyException.class)
public R handleDuplicateKeyException(DuplicateKeyException e){
log.error(e.getMessage(), e);
return R.error("数据库中已存在该记录");
}
@ExceptionHandler(AuthorizationException.class)
public R handleAuthorizationException(AuthorizationException e){
log.error(e.getMessage(), e);
return R.error("没有权限,请联系管理员授权");
}
@ExceptionHandler(Exception.class)
public R handleException(Exception e){
log.error(e.getMessage(), e);
ExceptionLogEntity exceptionLogEntity = handlerException(e);
exceptionLogMapper.insert(exceptionLogEntity);
return R.error();
}
private ExceptionLogEntity handlerException(Exception e) {
ExceptionLogEntity exceptionLogEntity = new ExceptionLogEntity();
if (e != null) {
if (e.getCause() != null) {
if (e.getCause().getMessage().length() > 1000) {
exceptionLogEntity.setCause(StringUtils.substring(e.getCause().getMessage(), 0, 1000));
} else {
exceptionLogEntity.setCause(e.getCause().getMessage());
}
}
if (e.getMessage() != null) {
if (e.getMessage().length() > 1000) {
exceptionLogEntity.setMessage(StringUtils.substring(e.getMessage(), 0, 1000));
} else {
exceptionLogEntity.setMessage(e.getMessage());
}
}
}
return exceptionLogEntity;
}
}