pr-str 还打印出跟踪消息
pr-str also prints out trace messages
我正在使用 pr-str
将 EDN 打印成字符串并与客户端通信。遇到非常有趣的行为,其中 pr-str
还输出 println
或 clojure.tools.trace/trace
消息混合到 EDN 字符串表示中。这是 pr-str
输出的字符串类型:
(TRACE from-ds {:key "asdasf", :weight 0, :tag "1"} ; trace message
{:key "asdasf", :weight 0, :tag "1"}) ; the actual edn that should be the sole output
我似乎无法在 REPL 中重现它。
为什么会这样?如何解决?
pr-str
使用 with-out-str
,它捕获在其块内发生的任何打印输出并将其转换为返回的字符串。
with-out-str
创建 *out*
的线程局部绑定。因此,您可以在输出字符串中获取日志输出的唯一方法是 pr-str
调用中的代码是否正在记录。这可以通过在 pr-str
调用之外绑定值然后对该值调用 pr-str
来解决。
例如
(let [the-value (generate-some-value) ; this prints something
value-string (pr-str the-value)] ; this doesn't have trace output
...)
我正在使用 pr-str
将 EDN 打印成字符串并与客户端通信。遇到非常有趣的行为,其中 pr-str
还输出 println
或 clojure.tools.trace/trace
消息混合到 EDN 字符串表示中。这是 pr-str
输出的字符串类型:
(TRACE from-ds {:key "asdasf", :weight 0, :tag "1"} ; trace message
{:key "asdasf", :weight 0, :tag "1"}) ; the actual edn that should be the sole output
我似乎无法在 REPL 中重现它。
为什么会这样?如何解决?
pr-str
使用 with-out-str
,它捕获在其块内发生的任何打印输出并将其转换为返回的字符串。
with-out-str
创建 *out*
的线程局部绑定。因此,您可以在输出字符串中获取日志输出的唯一方法是 pr-str
调用中的代码是否正在记录。这可以通过在 pr-str
调用之外绑定值然后对该值调用 pr-str
来解决。
例如
(let [the-value (generate-some-value) ; this prints something
value-string (pr-str the-value)] ; this doesn't have trace output
...)