配置Clojure日志以输出到nRepl

3

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

1个回答

0

默认情况下,控制台输出会打印在*nrepl-server*缓冲区中。因此,如果您配置clojure.tools.logging将输出打印到控制台,则可以在*nrepl-server*缓冲区中看到它。

我在CIDER文档中找不到更改此设置的方法。但是我发现了讨论,其中涉及到这个问题。提出了这样一个解决方案

;; 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*。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接