音色 `set-config!` 改变了 arity 因此不知道如何使用它来将 std err/out 输出到文件

timbre `set-config!` has changed arity thus don't know how to use it to output std err/out to a file

我正在尝试使用 https://github.com/ptaoussanis/timbre 登录到文件而不是控制台。这是我找到的一些关于如何做到这一点的文档:

; The default setup is simple console logging.  We with to turn off console logging and
; turn on file logging to our chosen filename.
(timbre/set-config! [:appenders :standard-out   :enabled?] false)
(timbre/set-config! [:appenders :spit           :enabled?] true)
(timbre/set-config! [:shared-appender-config :spit-filename] log-file-name)
(timbre/set-config! [:shared-appender-config :spit-filename] log-file-name)

这适用于以前版本的 sente,但不适用于版本 [com.taoensso/timbre“4.3.1”]。 (出于不相关的原因,我需要使用最新的)。上面代码的问题是 set-config! 现在接受一个参数——一个散列映射。而且我找不到任何可以帮助我将上述 'two params' 代码转换为新的 'one param' 代码的文档。

我知道有一个非常相似的问题there. This question has actual code in it so is more specific. I raised an issue as well. The code above basically comes straight from here

我得到了维护者的快速回复:

"Usage of the spit (file) appender is documented in the README at https://github.com/ptaoussanis/timbre#file-appender"

下面是回答问题的代码:

;; (:require [taoensso.timbre.appenders.core :as appenders]) ; Add to ns
(timbre/merge-config!  
    {:appenders {:println nil ; Remove println appender
                 :spit (appenders/spit-appender {:fname log-file-name})}})

不幸的是,即使使用 :println nil mapentry,相同的输出也会到达两个地方。所以这个答案是不正确的。

要在 timbre v4.0.0(2015 年 6 月 10 日)中登录到文件而不是控制台,您可以执行以下操作:

(ns app.core
  (:require [taoensso.timbre :as timbre]
            [taoensso.timbre.appenders.core :as appenders]))

;; Disable logging to the console in timbre v4.0.0:
(timbre/merge-config! {:appenders {:println {:enabled? false}}})

;; Create a "spit to file" appender in timbre v4.0.0: 
(timbre/merge-config! {:appenders {:spit (appenders/spit-appender {:fname "log.txt"})}})

请注意,在 timbre v4.3.1 中,以下内容不会删除或禁用 println(控制台)附加程序:

(timbre/merge-config! {:appenders {:println nil}})

相关问题可以在这里找到https://github.com/ptaoussanis/timbre/issues/163

加法: 由于 Timbre 允许修改它的配置映射 timbre/*config* 只需使用 标准的 clojure API,你也可以使用

(timbre/swap-config! assoc-in [:appenders :spit :enabled?] false)
(timbre/swap-config! assoc-in [:appenders :spit] (appenders/spit-appender {:fname "log.txt"}))

在此基础上你可以额外定义一个简单的helper:

(def set-log-config-param! (partial timbre/swap-config! assoc-in))

然后与v3.4.0 set-config的音色一致!句法你 在你的问题中引用了:

(set-log-config-param! [:appenders :println :enabled?] false)
(set-log-config-param! [:appenders :spit]   (appenders/spit-appender {:fname "log.txt"}))

恕我直言,这简化了音色 v3 和 v4 之间的配置转换,并为 参数比等价物更具可读性

(timbre/merge-config! {:appenders {:println {:enabled? false}}})
(timbre/merge-config! {:appenders {:spit    (appenders/spit-appender {:fname "log.txt"})}})