[main] StringFormatter: support formatting throwables
This commit is contained in:
parent
cc3070420a
commit
09073bb710
|
@ -1,5 +1,6 @@
|
|||
package io.gitlab.jfronny.commons;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class StringFormatter {
|
||||
|
@ -10,6 +11,21 @@ public class StringFormatter {
|
|||
return String.format(Locale.US, "%s", d);
|
||||
else
|
||||
return String.format(Locale.US, "%.0f", d);
|
||||
} else if (o instanceof Throwable t) {
|
||||
try {
|
||||
return t.getMessage() + getStackTrace(t);
|
||||
} catch (IOException e) {
|
||||
return t.toString();
|
||||
}
|
||||
} else return o.toString();
|
||||
}
|
||||
|
||||
public static String getStackTrace(Throwable t) throws IOException {
|
||||
try (ByteArrayOutputStream baot = new ByteArrayOutputStream()) {
|
||||
try (PrintStream ps = new PrintStream(baot, true)) {
|
||||
t.printStackTrace(ps);
|
||||
}
|
||||
return baot.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -100,17 +100,6 @@ public interface Logger {
|
|||
|
||||
default String format(String msg, Throwable t) {
|
||||
if (t == null) return msg;
|
||||
return msg + System.lineSeparator() + format(t);
|
||||
}
|
||||
|
||||
default String format(Throwable t) {
|
||||
try (ByteArrayOutputStream baot = new ByteArrayOutputStream()) {
|
||||
try (PrintStream ps = new PrintStream(baot, true)) {
|
||||
t.printStackTrace(ps);
|
||||
}
|
||||
return baot.toString();
|
||||
} catch (IOException e) {
|
||||
return t.toString();
|
||||
}
|
||||
return msg + System.lineSeparator() + StringFormatter.toString(t);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue