(require '[clojure.tools.logging :as log])
(def layer->multipart [{:name "layer-name" :content "veg"} {:name "layer-tag" :content "abs"}])
(def field->multipart [{:name "field-id" :content "12345"} {:name "field-version" :content "v1"}])
(log/infof "concat is %s" (concat layer->multipart field->multipart))
; => 2016-02-16 16:31:11,707 level=INFO [nREPL-worker-38] user:288 - concat is clojure.lang.LazySeq@87177bed
; WTF is clojure.lang.LazySeq@87177bed?
我已经检查了How to convert lazy sequence to non-lazy in Clojure的答案,这表明我需要做的就是 doall ,我所有的梦想都会成真。但是唉...不。
(log/infof "concat is %s" (doall (concat layer->multipart field->multipart)))
; => 2016-02-16 16:31:59,958 level=INFO [nREPL-worker-40] user:288 - concat is clojure.lang.LazySeq@87177bed
; still clojure.lang.LazySeq@87177bed is not what I wanted
我观察到(pr-str(concat layer-> multipart field-> multipart))做了我想要的,但没有任何意义; pr-str的文档说了一些关于" pr到字符串"并且pr的文档说"将对象打印到输出流,该输出流是* out *。"的当前值。我不想要任何事情* *,我只想要返回字符串值,以便记录器可以使用它!
(log/infof "concat is %s" (pr-str (concat layer->multipart field->multipart)))
; => 2016-02-16 16:42:02,927 level=INFO [nREPL-worker-1] user:288 - concat is ({:content "veg", :name "layer-name"} {:content "abs", :name "layer-tag"} {:content "12345", :name "field-id"} {:content "v1", :name "field-version"})
; this is what I wanted but I don't want anything going to *out*...or do I?
我需要做些什么才能获得pr-str变体的效果,而不必担心任何无意中被转移到stdout的事情(我猜这是* out *是什么)?我希望完全实现延迟序列以便进行日志记录(它永远不会变得太大......它最终只会像连续事故一样懒惰)。
如何记录LazySeq的全部值?
答案 0 :(得分:2)
问题是,在幕后,记录器在您的懒惰序列上调用.toString
。试试这个:
user=> (.toString (concat layer->multipart field->multipart))
;; "clojure.lang.LazySeq@87177bed"`
您真正想要的是将序列的内容转换为字符串。例如:
(log/infof "concat is %s" (apply str (concat layer->multipart field->multipart)))
;; Feb 16, 2016 5:10:19 PM clojure.tools.logging$eval420$fn__424 invoke
;; INFO: concat is {:name "layer-name", :content "veg"}{:name "layer-tag", :content "abs"}{:name "field-id", :content "12345"}{:name "field-version", :content "v1"}
顺便说一句,pr-str
也很好。正如它的名字所说,它打印到一个字符串,而不是*out*
。您正在使用该字符串。