How can I convert a stack trace to a string?

前端 未结 30 1428
再見小時候
再見小時候 2020-11-22 14:51

What is the easiest way to convert the result of Throwable.getStackTrace() to a string that depicts the stacktrace?

30条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 15:25

    My oneliner to convert stack trace to the enclosed multi-line string:

    Stream.of(e.getStackTrace()).map((a) -> a.toString()).collect(Collectors.joining("\n", "[", "]"))
    

    Easy to pass to the logger "as is".

提交回复
热议问题