Skip to content

Commit

Permalink
[#174] Smarter (faster) spit appender path creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaoussanis committed Jul 1, 2016
1 parent 0413b8a commit cd137ba
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions src/taoensso/timbre/appenders/core.cljx
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,6 @@

;;;; 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
(defn spit-appender
"Returns a simple `spit` file appender for Clojure."
Expand All @@ -78,14 +69,24 @@
:rate-limit nil
:output-fn :inherit
:fn
(fn [data]
(fn self [data]
(let [{:keys [output_]} data]
(try ; To allow TTL-memoization of dir creator
(ensure-spit-dir-exists! fname)
(try
(spit fname (str (force output_) "\n") :append true)
(catch java.io.IOException _))))})

(comment (spit-appender))
(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))))))))})

(comment
(spit-appender)
(let [f (:fn (spit-appender))]
(enc/qb 1000 (f {:output_ "boo"}))))

;;;; js/console appender (cljs only)

Expand Down

0 comments on commit cd137ba

Please sign in to comment.