最近的項目需要捕獲系統拋出的異常,並將異常信息保存,記錄以下解析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); } }