2016-12-04 70 views
1

我試圖從與Timbre相同的命名空間登錄到兩個不同的文件。或者如果這是不可能的,至少從兩個不同的命名空間的不同文件。在Timbre中記錄到兩個文件

檢查timbre/*config*我感覺我需要兩個配置圖來配置類似的東西。我可以創建另一個配置映射,並使用timbre/log*來代替標準配置映射,但我無法擺脫它不應該如何使用它的感覺......?

(timbre/log* timbre/*config* :info "Test with standard config") 

回答

2

據我所知,最簡單的方法的確是創建兩個配置地圖:

(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")) 

第二種方法是從吐附加器編寫自己的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))))))))}) 
+1

我想知道如何使用配置是爲了使用。謝謝,這工作! – Domchi