This commit is contained in:
Looly
2021-09-28 00:24:26 +08:00
parent 1072639f3b
commit f1b6d84fe7

View File

@@ -233,24 +233,25 @@ public class ExceptionUtil {
* 堆栈转为完整字符串 * 堆栈转为完整字符串
* *
* @param throwable 异常对象 * @param throwable 异常对象
* @param limit 限制最大长度 * @param limit 限制最大长度>0表示不限制长度
* @param replaceCharToStrMap 替换字符为指定字符串 * @param replaceCharToStrMap 替换字符为指定字符串
* @return 堆栈转为的字符串 * @return 堆栈转为的字符串
*/ */
public static String stacktraceToString(Throwable throwable, int limit, Map<Character, String> replaceCharToStrMap) { public static String stacktraceToString(Throwable throwable, int limit, Map<Character, String> replaceCharToStrMap) {
final FastByteArrayOutputStream baos = new FastByteArrayOutputStream(); final FastByteArrayOutputStream baos = new FastByteArrayOutputStream();
throwable.printStackTrace(new PrintStream(baos)); throwable.printStackTrace(new PrintStream(baos));
String exceptionStr = baos.toString();
int length = exceptionStr.length(); final String exceptionStr = baos.toString();
if (limit > 0 && limit < length) { final int length = exceptionStr.length();
length = limit; if (limit < 0 || limit > length) {
limit = length;
} }
if (MapUtil.isNotEmpty(replaceCharToStrMap)) { if (MapUtil.isNotEmpty(replaceCharToStrMap)) {
final StringBuilder sb = StrUtil.builder(); final StringBuilder sb = StrUtil.builder();
char c; char c;
String value; String value;
for (int i = 0; i < length; i++) { for (int i = 0; i < limit; i++) {
c = exceptionStr.charAt(i); c = exceptionStr.charAt(i);
value = replaceCharToStrMap.get(c); value = replaceCharToStrMap.get(c);
if (null != value) { if (null != value) {
@@ -261,6 +262,9 @@ public class ExceptionUtil {
} }
return sb.toString(); return sb.toString();
} else { } else {
if(limit == length){
return exceptionStr;
}
return StrUtil.subPre(exceptionStr, limit); return StrUtil.subPre(exceptionStr, limit);
} }
} }