配置clojure日志记录以输出到nRepl

时间:2016-01-03 11:40:48

标签: logging clojure

我正在寻找一种方法来配置clojure.tools.logging以向nRepl输出消息。我只找到一些输出到console的配置。

1 个答案:

答案 0 :(得分:0)

默认情况下,控制台输出打印在* nrepl-server * buffer。因此,如果您将clojure.tools.logging配置为打印输出到控制台,那么您可以在* nrepl-server * buffer上看到它。

我在CIDER文档中找不到改变它的方法。但我发现discussion关于这个问题。提出了这样的solution

;; run this code on the repl where you wish to see all output.
;; You will need to add the dependency [commons-io "2.4"] to your
;; leiningen dependencies.
(import 'org.apache.commons.io.output.WriterOutputStream)
(import 'java.io.PrintStream)

;; First, we redirect the raw stdout of the server to this repl
(System/setOut (PrintStream. (WriterOutputStream. *out*)
                             true)) ;; Auto-flush the PrintStream

;; Next, we alter the root binding of *out* so that new threads
;; send their output to THIS repl rather than the original System/out.
(alter-var-root #'*out* (fn [_] *out*))

;; Now the snippets should both send output to this repl:
(.println System/out "Hello stdout.")
(.start (Thread. #(println "Hello from a new thread.")))

但是在我的安装(CIDER 0.12.0,Clojure 1.8.0)上,它在(.start (Thread. #(println "Hello from a new thread.")))上抱怨:

error in process filter: [nREPL] No response handler with id nil found

但是,如果我不运行(alter-var-root #'*out* (fn [_] *out*)),则下面的打印示例效果很好,并将其输出打印到* cider-repl *,没有错误或警告。记录器输出也打印到* cider-repl *。