全局异常处理器实现系统异常日志记录到数据库

最近线上老是出现一些奇奇怪怪的问题,一直用XShell去查看又太麻烦。原来的老项目没有做异常日志记录,所以就想着把这块给补上。
参考原文全局异常处理器实现系统异常日志记录到数据库
然后结合自己的实际情况做了一些修改。前端实现为layui
实现效果如下:
在这里插入图片描述
在这里插入图片描述
具体步骤:

1 数据库

这边还是照搬原文的,但是有些字段没有用上,有些业务也可以结合自己的情况再增加操作人的字段,这样也能定位是谁操作,怎么导致的异常

CREATE TABLE `sys_exception_log` (
  `id` varchar(32) NOT NULL COMMENT '主键id',
  `ip` varchar(20) DEFAULT NULL COMMENT '远程访问主机IP',
  `service_name` varchar(32) NOT NULL COMMENT '服务名称',
  `class_name` varchar(255) DEFAULT NULL COMMENT '类名',
  `method_name` varchar(120) DEFAULT NULL COMMENT '方法名',
  `exception_type` varchar(255) DEFAULT NULL COMMENT '异常类型',
  `exception_msg` text COMMENT '异常信息',
  `create_time` datetime NOT NULL COMMENT '异常发生时间',
  `is_view` tinyint(2) DEFAULT '1' COMMENT '是否查看,1:未查看、2:已查看',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='异常信息日志表';

2、接口

列表、详情、删除都是普通的增删改查,这边就不做介绍了

3、全局处理器


/**
 * 异常处理
 */
public class CustomExceptionResolver implements HandlerExceptionResolver {

    private static Logger logger = LoggerFactory.getLogger(CustomExceptionResolver.class);

    @Autowired
    private ExceptionLogService exceptionLogService;

    @Override
    public ModelAndView resolveException(HttpServletRequest request,
                                         HttpServletResponse response, Object handler, Exception ex) {

        // 系统异常,记录异常信息到数据库
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            String className = handlerMethod.getBeanType().getName();
            String methodName = handlerMethod.getMethod().getName();
            StringWriter sw = new StringWriter();
            ex.printStackTrace(new PrintWriter(sw, true));

            SysExceptionLog log = new SysExceptionLog();
            log.setId(UuidUtil.getUuid());
            log.setServiceName("(后台)");
            log.setClassName(className);
            log.setMethodName(methodName);
            log.setExceptionType(ex.getClass().getSimpleName());
            log.setExceptionMsg(sw.toString());
            log.setCreateTime(new Date());
            exceptionLogService.save(log);
        }

        CustomException customException = null;
        if(ex instanceof CustomException) {
            customException = (CustomException) ex;
        } else {
            logger.error("unknown error", ex);

            customException = new CustomException("系统未知错误:" + ex.getMessage());
        }

        Map<String,Object> errorInfo = new HashMap<>(3);
        errorInfo.put("msg", customException.getMessage());
        errorInfo.put("cause", JSON.toJSONString(customException.getCause()));

        try {
            response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            response.setContentType(MediaType.APPLICATION_JSON_VALUE);
            response.setCharacterEncoding("UTF-8"); //避免乱码
            response.setHeader("Cache-Control", "no-cache, must-revalidate");
            response.getWriter().write(JSON.toJSONString(errorInfo));
        } catch (IOException e) {
            e.printStackTrace();
        }

        ModelAndView modelAndView = new ModelAndView();
        return modelAndView;
    }
}

只记录的话只做到这边就够了,如果还要实现邮件那些的话参考原文即可

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值