Get stacktrace as string
if number23_cn's solution is a bit much, this is how you can use the result of .getStackTrace as a string (which can then be printed, put in a log, whatever)
(try (/ 1 0)
(catch Throwable t
(map str (.getStackTrace t))))
use clojure.repl.pst
get StackTrace, and binding *err*
to java.io.StringWriter
:
(use '[clojure.repl :only (pst)])
(defmacro with-err-str
"Evaluates exprs in a context in which *err* is bound to a fresh
StringWriter. Returns the string created by any nested printing
calls."
[& body]
`(let [s# (new java.io.StringWriter)]
(binding [*err* s#]
~@body
(str s#))))
(try
(/ 1 0)
(catch Exception e
(let [s (with-err-str (pst e 36))]
(println "Error log:")
(println s))))
Here's a slight improvement over noisesmith's answer. It doesn't leave a lazy seq and has a beautification feature:
(apply str (interpose "\n" (.getStackTrace t)))