Java 獲取堆棧報錯信息(String)

/**
 * 錯誤堆棧信息轉String
 * @param t
 * @return
 */
public static String getStackTrace(Throwable t){
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    String stackMsg ;
    try{
        t.printStackTrace(pw);
        stackMsg = sw.toString();
        int casedInt = stackMsg.lastIndexOf("Caused by:");
        if (casedInt != -1){
            stackMsg = stackMsg.substring(casedInt, stackMsg.length());
            if (stackMsg.split("at").length > 15){
                Pattern p = Pattern.compile("at");
                Matcher m = p.matcher(stackMsg);
                int num = 0, index = -1;
                while(m.find()){
                    num++;
                    if(15 == num){
                        index = m.start();
                        System.out.println(m.start());
                        stackMsg = "</br>"+stackMsg.substring(0, index);
                        break;
                    }
                }
            }
        }
        return stackMsg;
    }catch (Exception e){
        e.printStackTrace();
    }finally{
        pw.close();
    }

    return sw.toString();
}
相關文章
相關標籤/搜索