Skip to content

Commit

Permalink
[#308 #320] Add cljs spit-appender 3rd-party appender for running on …
Browse files Browse the repository at this point in the history
…node (@mavines)
  • Loading branch information
Mason Vines authored and ptaoussanis committed Oct 1, 2020
1 parent 45fb2d1 commit 791f8b2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
4 changes: 3 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
[raven-clj "1.6.0"]
[congomongo "2.2.1"]
[server-socket "1.0.0"]
[org.zeromq/cljzmq "0.1.4"]]}
[org.zeromq/cljzmq "0.1.4"]
[cljs-node-io "1.1.2"] ; Node spit appender
]}

:test
{:dependencies
Expand Down
30 changes: 30 additions & 0 deletions src/taoensso/timbre/appenders/community/node_spit.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
(ns taoensso.timbre.appenders.community.node-spit
"Requires https://github.com/pkpkpk/cljs-node-io."
{:author "Mason Vines (@mavines)"}
(:require
[taoensso.encore :as enc]
[cljs-node-io.core :as nio]))

(defn node-spit-appender
"Returns a simple `spit` file appender for `cljs-node-io`.
Based on `taoensso.timbre.appenders.core/spit-appender`."
[& [{:keys [fname append?]
:or {fname "./timbre-spit.log"
append? true}}]]

(enc/have? enc/nblank-str? fname)

{:enabled? true
:fn
(fn self [data]
(let [{:keys [output_]} data]
(try
(nio/spit fname (str (force output_) enc/system-newline)
:append append?)

(catch :default e
(if (:spit-appender/retry? data)
(throw e)
(do
(nio/make-parents fname)
(self (assoc data :spit-appender/retry? true))))))))})
21 changes: 7 additions & 14 deletions src/taoensso/timbre/appenders/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -80,31 +80,24 @@
append? true
locking? true}}]]

{:enabled? true
:async? false
:min-level nil
:rate-limit nil
:output-fn :inherit
:fn
(let [lock (Object.) ; For thread safety, Ref. #251
system-newline enc/system-newline]
(have? enc/nblank-str? fname)

{:enabled? true
:fn
(let [lock (Object.)]
(fn self [data]
(let [{:keys [output_]} data]
(try
(when locking? (monitor-enter lock))
(when locking? (monitor-enter lock)) ; For thread safety, Ref. #251
(with-open [^java.io.BufferedWriter w (jio/writer fname :append append?)]
(.write w ^String (force output_))
(.newLine w))

(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))
(do
(jio/make-parents fname)
(self (assoc data :spit-appender/retry? true)))))

(finally
Expand Down

0 comments on commit 791f8b2

Please sign in to comment.