Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get stacktrace as string

I'm using Clojure and I want to get my hands on a stack trace that I can log (ideally, i would like to get it as a String).

I see that (.getStackTrace e) returns a StackTraceElement[] but I don't know how to print something meaningful out of it. My second approach was (.printStackTrace e) with a PrintWriter as a parameter (because I know this is possible in Java), but I don't seem to get the correct syntax.

Thanks.

like image 986
sebi Avatar asked Jun 26 '13 07:06

sebi


People also ask

How do you get Stacktrace strings?

Example: Convert stack trace to a stringIn the catch block, we use StringWriter and PrintWriter to print any given output to a string. We then print the stack trace using printStackTrace() method of the exception and write it in the writer. Then, we simply convert it to string using toString() method.

How do I get Stacktrace from exception?

getStackTrace(); String exception = ""; for (StackTraceElement s : stack) { exception = exception + s. toString() + "\n\t\t"; } System. out. println(exception); // then you can send the exception string to a external file. }

How do you print Stacktrace in logs?

To print a stack trace to log you Should declare logger and method info(e. toString()) or log(Level.INFO, e. toString()). Logging is the process of writing log messages during the execution of a program to get error and warning messages as well as info messages.

How do you analyze a Stacktrace?

From the Analyze menu, click Analyze Stack Trace. Paste the stack trace text into the Analyze Stack Trace window and click OK. Android Studio opens a new <Stacktrace> tab with the stack trace you pasted under the Run window.


1 Answers

You can just use the very useful pr-str function from clojure.core:

(catch Exception e
      (l/error "Ho no, an exception:" (pr-str e)))
#error {
 :cause nil
 :via
 [{:type java.lang.NullPointerException
   :message nil
   :at [my_app$fn__47429$fn__47430 invoke "my_app.clj" 30]}]
 :trace
 [[my_app$fn__47429$fn__47430 invoke "my_app.clj" 30]
  [my_app$my_func invokeStatic "my_app.clj" 13]
  [my_app$my_func invoke "my_app.clj" 10]
  [my_app$other_func$fn__29431 invoke "my_app.clj" 19]
  [my_app$other_func_BANG_ invokeStatic "my_app.clj" 28]
  [my_app$other_func_BANG_ invoke "my_app.clj" 27]
  [my_app$yet_another_func invokeStatic "my_app.clj" 40]
  [my_app$yet_another_func invoke "my_app.clj" 37]
  [clojure.core$fn__8072$fn__8074 invoke "core.clj" 6760]
  [clojure.core.protocols$iter_reduce invokeStatic "protocols.clj" 49]
  [clojure.core.protocols$fn__7839 invokeStatic "protocols.clj" 75]
  [clojure.core.protocols$fn__7839 invoke "protocols.clj" 75]
  [clojure.core.protocols$fn__7781$G__7776__7794 invoke "protocols.clj" 13]
  [clojure.core$reduce invokeStatic "core.clj" 6748]
  [clojure.core$fn__8072 invokeStatic "core.clj" 6750]
  [clojure.core$fn__8072 invoke "core.clj" 6750]
  [clojure.core.protocols$fn__7860$G__7855__7869 invoke "protocols.clj" 175]
  [clojure.core$reduce_kv invokeStatic "core.clj" 6776]
  [clojure.core$reduce_kv invoke "core.clj" 6767]
  [my_app$yet_another_func invokeStatic "data_streamer.clj" 48]
  [my_app$yet_another_func invoke "data_streamer.clj" 47]
  [my_app$other_func invokeStatic "data_streamer.clj" 66]
  [my_app$other_func invoke "data_streamer.clj" 58]
  [my_app$other_func$fn__48385 invoke "my_app.clj" 73]
  [clojure.core.async$thread_call$fn__6553 invoke "async.clj" 442]
  [clojure.lang.AFn run "AFn.java" 22]
  [java.util.concurrent.ThreadPoolExecutor runWorker "ThreadPoolExecutor.java" 1135]
  [java.util.concurrent.ThreadPoolExecutor$Worker run "ThreadPoolExecutor.java" 635]
  [java.lang.Thread run "Thread.java" 844]]}
like image 191
Eyal Avatar answered Oct 12 '22 14:10

Eyal