What is the easiest way to convert the result of Throwable.getStackTrace()
to a string that depicts the stacktrace?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Print the stacktrace to a PrintStream, then convert it to a String
Warning: This may be a bit off topic, but oh well... ;)
I don't know what the original posters reason was for wanting the stack trace as string in the first place. When the stack trace should end up in an SLF4J/Logback LOG, but no exception was or should be thrown here's what I do:
I like it because it does not require an external library (other than your logging backend, which will be in place most of the time anyway, of course).
For me the cleanest and easiest way was:
The following code allows you to get the entire stackTrace with a
String
format, without using APIs like log4J or evenjava.util.Logger
:Kotlin
Extending the Throwable class will give you the String property
error.stackTraceString
:an exapansion on Gala's answer that will also include the causes for the exception: