将异常堆栈信息转换成字符串

package cn.com.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

public class ExceptionMsgUtils {

    /**
     * getExceptionInfo
     * @param e
     * @return result限制长度30000,MySQL text字段长65535,防止存不进去
     */
    public static String getExceptionInfo(Exception e) {
        StringWriter sw = null;
        PrintWriter pw = null;
        try {
            sw = new StringWriter();
            pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            String result = sw.toString();
            if(StringUtils.isNotBlank(result) && result.length() > 30000) {
                result = result.substring(0,30000);
            }
            return result;
        } finally {
            if (null != sw) {
                try {
                    sw.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            if (null != pw) {
                pw.close();
            }
        }
    }
}
posted @ 2019-10-12 10:07  Bevis  阅读(780)  评论(0编辑  收藏  举报