美文网首页
系统添加异常日志配置

系统添加异常日志配置

作者: 初心myp | 来源:发表于2019-07-31 14:16 被阅读0次
目标:

每个系统都会有相应的日志,那么在这里就和大家分享一下,我做过的项目里,异常日志都是怎么记录入库的

首先我们记录的异常日志都是需要落库保存的,所以需要数据库表的支持。然后在每次捕捉异常的时候,都需要调用方法,记录落库,然后在后台的管理系统中就可以展示对应的异常日志数据了。

分享一下数据设计:

#创建表的sql
create table exception_log
(
   id                   bigint not null auto_increment comment '主键id(自增)',
   operator_code        varchar(64) not null default '' comment '操作人id',
   operator             varchar(50) not null default '' comment '操作人',
   uri                  varchar(500) not null default '' comment '请求路径',
   operator_ip          varchar(64) not null default '' comment '操作人ip',
   description          varchar(200) not null default '' comment '错误描述',
   host_name            varchar(64) not null default '' comment '主机',
   detail               text not null default '' comment '错误详细',
   brower_message       varchar(200) not null default '' comment '浏览器信息',
   create_time          datetime not null default CURRENT_TIMESTAMP comment '创建时间',
   primary key (id)
);

然后创建对应的实体类、业务层接口和实现类、持久化层的接口和xml文件,提供新增和查询的方法就可以了。

在方法里面调用方法,具体下面举例说明

//日志的业务接口
@Autowired
private ExceptionLogService exceptionLogService;

@PostMapping("add")
public void add(HttpServletRequest request){
    try{
        System.out.println("正常的逻辑代码");
    } catch(Exception e){
        exceptionLogService.add("异常描述(比如:添加***异常)", e, request);
    }
}
//ExceptionLogService接口
//入参:异常描述,异常信息,请求
void handler(String exceptionMsg, Exception e, HttpServletRequest request);
//ExceptionLogServiceImpl实现类方法
public class ExceptionServiceImpl implements ExceptionService {
    
    protected static final Logger LOGGER = LoggerFactory.getLogger(ExceptionServiceImpl.class);

    @Value("${chuxin.example.serverName}")
    private String  serverName;
    
    @Override
    public void handler(String exceptionMsg, Exception e, HttpServletRequest request) {
        User user = new User();//这里是获取当前登录人信息
        try {
            // 如果异常时用来提示的,那么什么都不做
            if(e instanceof ExampleException){
                return;
            }
            ExceptionLog exceptionLog = new ExceptionLog();
            exceptionLog.setServerName(serverName);
            exceptionLog.setOperator(serverName);
            exceptionLog.setDescription(exceptionMsg);
            if (request != null) {
                exceptionLog.setOperatorIp(NetUtils.getIpAddr(request));
                exceptionLog.setUri(request.getRequestURI());
                exceptionLog.setBrowerMessage(request.getHeader("User-Agent"));
            }
            if (user != null) {
                exceptionLog.setOperatorCode(StringUtils.defaultString(user.getEmplId()));
            }
            //输出错误堆栈
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            PrintStream ps = new PrintStream(outputStream);
            e.printStackTrace(ps);
            exceptionLog.setDetail(outputStream.toString());
            exceptionLog.setCreateTime(new Date());
            exceptionLog.setHostName(InetAddress.getLocalHost().getHostName());
            exceptionLogService.insert(exceptionLog);
        } catch (Exception e1) {
            LOGGER.warn("存储异常日志失败");
        }
    }
}

解释:
ExampleException是自定义异常,一般这种自定义异常都是用来提示的,所以不需要记录落库的。
User是获取当前用户信息,记录当前谁操作的
serverName是操作系统(比如,ecm、web、app等),在配置文件中配置的

#配置文件中的serverName写法
chuxin:
  example:
    serverName: ECM

自定义异常类:

public class ExampleException extends RuntimeException {
    private int code;

    public ExampleException(String message) {
        super(message);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }
}

这样我们就可以完成异常日志的记录工作了。。。


上述代码中使用到的工具类如下:

import javax.servlet.http.HttpServletRequest;

/**
 * <p>Description: [网络工具类]</p>
 * Created on 2018/3/28
 * @version 1.0
 */
public class NetUtils {

    /**
     * 获取真实的ip
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}

相关文章

  • 系统添加异常日志配置

    目标:每个系统都会有相应的日志,那么在这里就和大家分享一下,我做过的项目里,异常日志都是怎么记录入库的 首先我们记...

  • SpringBoot脱离SpringCloud使用Feign

    快速导航 [添加依赖] 添加依赖 [添加核心配置类] 添加核心配置类TestFeignConfig [添加日志配置...

  • Django日志配置

    日志用于记录或收集系统运行过程中的各种日志信息 在项目配置文件中添加日志器配置 日志一般位于项目根目录下的logs...

  • 系统添加操作日志配置

    目标:每个系统都会有相应的日志,那么在这里就和大家分享一下,我做过的项目里,操作日志都是怎么记录入库的 首先我们记...

  • elk分析nginx日志

    日志格式示例 nginx日志示例 配置logstash logstash 配置文件 添加pattern_dir ...

  • ELK分析tomcat 业务日志

    日志格式示例 tomcat错误日志示例 配置logstash logstash 配置文件 添加pattern_di...

  • Spring 全局异常、数据绑定

    全局异常处理 异常处理类配置 Controllere层 参数数据解析 在配置中添加

  • CtenOS 7 安装 MongoDB

    安装wget 下载 解压 移动 创建数据,日志,配置目录 添加环境配置 在配置文件末尾添加 重新加载配置,使配置生...

  • 服务启动时出现大量 Hystrix timed-out 第一次排

    异常日志 有人说是要开启 Ribbon 饥饿加载,看了下服务配置,已经开启了。 排查过程 因为服务启动时添加了 J...

  • log4j2配置

    log4j2配置 添加依赖包 添加配置文件log4j2.xml到resources文件中 测试日志 配置自定义日志级别

网友评论

      本文标题:系统添加异常日志配置

      本文链接:https://www.haomeiwen.com/subject/nnkzrctx.html