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

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

本文介绍了配置Clojure日志记录以输出到nRepl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking for a way to config clojure.tools.logging to output messages to nRepl. I only find some configuration to output to console.

推荐答案

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

By default, console output printed at *nrepl-server* buffer. So if you configure clojure.tools.logging to print output to console then you can see it on *nrepl-server* buffer.

我在CIDER文档中找不到更改此方法的方法.但是我发现与此问题有关的讨论.提出了这样一种解决方案:

I could not find a way to change this in CIDER documentation. But I found discussion concerning this question. There is proposed such a 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."))):

But on my installation (CIDER 0.12.0, Clojure 1.8.0) it complains on (.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 *.

However if I don't run (alter-var-root #'*out* (fn [_] *out*)) then printing examples below works good and prints its output to *cider-repl* without errors or warnings. Logger output also printed to *cider-repl*.

这篇关于配置Clojure日志记录以输出到nRepl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 05:33