最近的项目需要捕获系统抛出的异常,并将异常信息保存,记录以下解析Exception的方法。

 


 

  • 异常详细信息
    这里说的“异常详细信息”指的是平时打印到控制台的那种信息,如下图

 

获取方法:

package com.pantech.boot.common.systemlog.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;

/**
 * @author 肖政宇
 * @date 2019-10-30 11:11
 * 说明:异常解析
 */
public class ExceptionInformation {

    /**
     * 异常解析
     */
    public static String getExceptionInformation(Exception exception) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PrintStream printStream = new PrintStream(outputStream);
        exception.printStackTrace(printStream);

        String exceptionInformation = new String(outputStream.toByteArray());
        printStream.close();
        try {
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return exceptionInformation;
    }
}

 


 

  • 异常具体信息

这里说的异常具体信息指的是,具体的某一项信息,比如异常从哪个类抛出、异常用哪个方法抛出、产生异常的代码在第几行

 

package com.pantech.boot.module.log.serviceimpl;

import com.pantech.boot.common.systemlog.SystemException;
import com.pantech.boot.common.systemlog.util.ExceptionInformation;
import com.pantech.boot.common.systemlog.util.IpAddress;
import com.pantech.boot.module.log.entity.SystemExceptionLogEntity;
import com.pantech.boot.module.log.entity.SystemOperationLogEntity;
import com.pantech.boot.module.log.repository.SystemExceptionLogRepository;
import com.pantech.boot.module.log.service.SystemExceptionLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 肖政宇
 * @date 2019-10-29 17:06
 * 说明:系统异常记录
 */
@Service
public class SystemExceptionLogServiceImpl implements SystemExceptionLogService {
    private final Logger logger = LoggerFactory.getLogger(SystemExceptionLogServiceImpl.class);

    private SystemExceptionLogRepository repository;
    private IpAddress ipAddress;

    @Autowired
    public void setRepository(SystemExceptionLogRepository repository) {
        this.repository = repository;
    }

    @Autowired
    public void setIpAddress(IpAddress ipAddress) {
        this.ipAddress = ipAddress;
    }

    /**
     * 保存一条系统异常
     *
     * @param systemExceptionLog - 异常信息
     */
    @Override
    public SystemExceptionLogEntity save(SystemExceptionLogEntity systemExceptionLog) {
        return repository.save(systemExceptionLog);
    }

    /**
     * 用于手动添加异常日志
     *
     * @param e - 异常类型
     */
    @Override
    public SystemExceptionLogEntity exceptionLog(Exception e) {
        /**
         * 1、解析异常信息
         */
        //获取异常栈首个元素,用以解析异常部分信息
        StackTraceElement stackTraceElement = e.getStackTrace()[0];
        //异常类型
        String exceptionType = e.toString();
        //异常抛出于某个类
        String className = stackTraceElement.getClassName();
        //异常抛出于某个方法
        String methodName = stackTraceElement.getMethodName();
        //异常抛出于第几行
        int lineNumber = stackTraceElement.getLineNumber();
        //异常抛出地点详细信息
        String throwPlace = className + "." + methodName + "[" + lineNumber + "]";
        //异常详细信息
        String content = ExceptionInformation.getExceptionInformation(e);
        String[] con = content.split("\n");
        if (con.length > 50) {
            //保存前50行
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < 50; i++) {
                stringBuilder.append(con[i]);
                stringBuilder.append("\n");
            }
            content = stringBuilder.toString();
        }
        //ip地址
        String ip = ipAddress.getIpAddress();
        /**
         * 2、保存异常信息到数据库
         */
        SystemExceptionLogEntity systemExceptionLog = new SystemExceptionLogEntity();
        systemExceptionLog.setExceptionType(exceptionType);
        systemExceptionLog.setIpAddress(ip);
        systemExceptionLog.setThrowPlace(throwPlace);
        systemExceptionLog.setContent(content);
        return this.save(systemExceptionLog);
    }
}