Clojure:在go块或线程中执行代码时登录到控制台

时间:2015-09-22 11:38:56

标签: clojure core.async

随着许多go块一下子进入,所有写入控制台,当文本到达控制台时,文本可能会混乱/混合。如何避免这种情况,因此当从go块中发出跟踪输出时,控制台中的跟踪输出是否正确?

2 个答案:

答案 0 :(得分:2)

这个答案使用core.async本身。以下是谈话:

;;;;; Logging Handler ;;;;;

(def log-chan (chan))

(thread
 (loop []
   (when-let [v (<!! log-chan)]
     (println v)
     (recur)))
 (println "Log Closed"))


(close! log-chan)

(defn log [msg]
  (>!! log-chan msg))

(log "foo")

here

逐字复制的代码

Timothy Balridge的谈话是here

我有一个atom用于打开和关闭调试。要准确显示显示的消息,用法如下:

(u/log @debug (str "Asked " info-ele ", and got back: " some-return))

在另一端像这样:

(defn log [debug msg] 
  (when debug (>!! log-chan msg)))

答案 1 :(得分:1)

使用core.async通道序列化所有日志记录事件将起作用,但更标准的方法是使用logbacklog4j之类的日志记录框架。它们都是为从多个线程记录事件而设计的(这实际上是从core.async go块中记录时发生的事情。)

Best practices for Java logging from multiple threads?