有时候想获取异常日志详细信息上报(具体哪个类哪一行出现异常),可以通过以下方式:
private static String getStackMsg(Exception e) {
StringBuffer sb = new StringBuffer();
StackTraceElement[] stackArray = e.getStackTrace();
for (int i = 0; i < stackArray.length; i++) {
StackTraceElement element = stackArray[i];
sb.append(element.toString() + "\n");
}
return sb.toString();
}
private static String getStackMsg(Throwable e) {
StringBuffer sb = new StringBuffer();
StackTraceElement[] stackArray = e.getStackTrace();
for (int i = 0; i < stackArray.length; i++) {
StackTraceElement element = stackArray[i];
sb.append(element.toString() + "\n");
}
return sb.toString();
}