思路:获取异常流,将异常流输出为String
public static String stringifyException(final Throwable e) {
if (e == null) {
return "(nothing throwable)";
}
try {
StringWriter stm = new StringWriter();
PrintWriter wrt = new PrintWriter(stm);
e.printStackTrace(wrt);
wrt.close();
return stm.toString();
} catch (Throwable t) {
return e.getClass().getName() + " (error while printing stack trace)";
}
}
通过上述工具类,我们可以获取你的异常堆栈信息,调用告警接口,直接在告警邮件或通知中看到堆栈信息。(下图例子中,我是直接error输出了,生产或测试环境中替换为你的邮件接口即可)