在 Timbre 中记录到两个文件
Logging to two files in Timbre
我正在尝试使用 Timbre 从同一命名空间登录到两个不同的文件。或者如果那不可能,至少从两个不同的命名空间到不同的文件。
检查 timbre/*config*
我的印象是我需要两个配置图来配置类似的东西。我可以创建另一个配置映射并将其与 timbre/log*
一起使用来代替标准配置映射,但我无法摆脱这种不应该如何使用的感觉...?
(timbre/log* timbre/*config* :info "Test with standard config")
据我所知,最简单的方法确实是创建两个配置映射:
(def config1
{:level :debug
:appenders {:spit1 (appenders/spit-appender {:fname "file1.log"})}})
(def config2
{:level :debug
:appenders {:spit2 (appenders/spit-appender {:fname "file2.log"})}})
(timbre/with-config config1
(info "This will print in file1") )
(timbre/with-config config2
(info "This will print in file2") )
第二种方法是从 spit-appender 编写自己的 appender:
https://github.com/ptaoussanis/timbre/blob/master/src/taoensso/timbre/appenders/core.cljx
(defn my-spit-appender
"Returns a simple `spit` file appender for Clojure."
[& [{:keys [fname] :or {fname "./timbre-spit.log"}}]]
{:enabled? true
:async? false
:min-level nil
:rate-limit nil
:output-fn :inherit
:fn
(fn self [data]
(let [{:keys [output_]} data]
(try
;; SOME LOGIC HERE TO CHOOSE THE FILE TO OUTPUT TO ...
(spit fname (str (force output_) "\n") :append true)
(catch java.io.IOException e
(if (:__spit-appender/retry? data)
(throw e) ; Unexpected error
(let [_ (have? enc/nblank-str? fname)
file (java.io.File. ^String fname)
dir (.getParentFile (.getCanonicalFile file))]
(when-not (.exists dir) (.mkdirs dir))
(self (assoc data :__spit-appender/retry? true))))))))})
我正在尝试使用 Timbre 从同一命名空间登录到两个不同的文件。或者如果那不可能,至少从两个不同的命名空间到不同的文件。
检查 timbre/*config*
我的印象是我需要两个配置图来配置类似的东西。我可以创建另一个配置映射并将其与 timbre/log*
一起使用来代替标准配置映射,但我无法摆脱这种不应该如何使用的感觉...?
(timbre/log* timbre/*config* :info "Test with standard config")
据我所知,最简单的方法确实是创建两个配置映射:
(def config1
{:level :debug
:appenders {:spit1 (appenders/spit-appender {:fname "file1.log"})}})
(def config2
{:level :debug
:appenders {:spit2 (appenders/spit-appender {:fname "file2.log"})}})
(timbre/with-config config1
(info "This will print in file1") )
(timbre/with-config config2
(info "This will print in file2") )
第二种方法是从 spit-appender 编写自己的 appender:
https://github.com/ptaoussanis/timbre/blob/master/src/taoensso/timbre/appenders/core.cljx
(defn my-spit-appender
"Returns a simple `spit` file appender for Clojure."
[& [{:keys [fname] :or {fname "./timbre-spit.log"}}]]
{:enabled? true
:async? false
:min-level nil
:rate-limit nil
:output-fn :inherit
:fn
(fn self [data]
(let [{:keys [output_]} data]
(try
;; SOME LOGIC HERE TO CHOOSE THE FILE TO OUTPUT TO ...
(spit fname (str (force output_) "\n") :append true)
(catch java.io.IOException e
(if (:__spit-appender/retry? data)
(throw e) ; Unexpected error
(let [_ (have? enc/nblank-str? fname)
file (java.io.File. ^String fname)
dir (.getParentFile (.getCanonicalFile file))]
(when-not (.exists dir) (.mkdirs dir))
(self (assoc data :__spit-appender/retry? true))))))))})