【问题标题】:Get stacktrace as string以字符串形式获取堆栈跟踪
【发布时间】:2013-06-23 05:47:41
【问题描述】:

我正在使用 Clojure,我想获得一个可以记录的堆栈跟踪(理想情况下,我想将它作为一个字符串来获取)。

我看到(.getStackTrace e) 返回一个StackTraceElement[],但我不知道如何打印出有意义的东西。我的第二种方法是(.printStackTrace e) 使用 PrintWriter 作为参数(因为我知道这在 Java 中是可能的),但我似乎没有得到正确的语法。

谢谢。

【问题讨论】:

    标签: exception clojure stack-trace printstacktrace


    【解决方案1】:

    使用clojure.repl.pst 获取StackTrace,并将*err* 绑定到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))))
    

    【讨论】:

    • 像魅力一样工作。只是我很乐意将第一条语句更改为 (use [clojure.repl :only (pst)])
    【解决方案2】:

    如果 number23_cn 的解决方案有点多,这就是你如何将 .getStackTrace 的结果用作字符串(然后可以打印,放入日志等)

    (try (/ 1 0)
      (catch Throwable t
        (map str (.getStackTrace t))))
    

    【讨论】:

    • (apply str (interpose "\n" (.getStackTrace t))) 也很好用。
    【解决方案3】:

    这里比noisesmith 的回答略有改进。不留lazy seq,有美化功能:

    (apply str (interpose "\n" (.getStackTrace t)))
    

    【讨论】:

      【解决方案4】:

      还有clojure.stacktrace,它具有print-stack-trace、print-trace-element和其他一些有用的功能。

      【讨论】:

        【解决方案5】:

        你可以使用with-out-str

        (try
          (name nil)
          (catch Exception e
            (with-out-str (println e))))
        

        【讨论】:

          【解决方案6】:

          你可以使用clojure.core中非常有用的pr-str函数:

          (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]]}
          

          【讨论】:

            猜你喜欢
            • 2013-09-04
            • 2010-10-11
            • 2011-05-22
            • 1970-01-01
            • 1970-01-01
            • 2016-02-26
            • 2010-11-20
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多