mirror of https://github.com/status-im/timbre.git
[#174] Smarter (faster) spit appender path creation
This commit is contained in:
parent
ba6bc3f79c
commit
f399d250cb
|
@ -59,15 +59,6 @@
|
||||||
|
|
||||||
;;;; Spit appender (clj only)
|
;;;; Spit appender (clj only)
|
||||||
|
|
||||||
#+clj
|
|
||||||
(def ^:private ensure-spit-dir-exists!
|
|
||||||
(enc/memoize* (enc/ms :mins 1)
|
|
||||||
(fn [fname]
|
|
||||||
(when-not (str/blank? fname)
|
|
||||||
(let [file (java.io.File. ^String fname)
|
|
||||||
dir (.getParentFile (.getCanonicalFile file))]
|
|
||||||
(when-not (.exists dir) (.mkdirs dir)))))))
|
|
||||||
|
|
||||||
#+clj
|
#+clj
|
||||||
(defn spit-appender
|
(defn spit-appender
|
||||||
"Returns a simple `spit` file appender for Clojure."
|
"Returns a simple `spit` file appender for Clojure."
|
||||||
|
@ -78,14 +69,24 @@
|
||||||
:rate-limit nil
|
:rate-limit nil
|
||||||
:output-fn :inherit
|
:output-fn :inherit
|
||||||
:fn
|
:fn
|
||||||
(fn [data]
|
(fn self [data]
|
||||||
(let [{:keys [output_]} data]
|
(let [{:keys [output_]} data]
|
||||||
(try ; To allow TTL-memoization of dir creator
|
(try
|
||||||
(ensure-spit-dir-exists! fname)
|
|
||||||
(spit fname (str (force output_) "\n") :append true)
|
(spit fname (str (force output_) "\n") :append true)
|
||||||
(catch java.io.IOException _))))})
|
(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))]
|
||||||
|
|
||||||
(comment (spit-appender))
|
(when-not (.exists dir) (.mkdirs dir))
|
||||||
|
(self (assoc data :__spit-appender/retry? true))))))))})
|
||||||
|
|
||||||
|
(comment
|
||||||
|
(spit-appender)
|
||||||
|
(let [f (:fn (spit-appender))]
|
||||||
|
(enc/qb 1000 (f {:output_ "boo"}))))
|
||||||
|
|
||||||
;;;; js/console appender (cljs only)
|
;;;; js/console appender (cljs only)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue