From bf619cbe6e15c974421264e075cb9cdb87695c81 Mon Sep 17 00:00:00 2001 From: Peter Taoussanis Date: Tue, 19 Jul 2016 11:26:17 +0700 Subject: [PATCH] [#183] Add support for appender-level middleware Motivating use case: let users apply custom ns-filtering or other conditional logic at an appender level *without* needing to actually modify the appender fn. This is just a natural generalization of the recently added support for appender-level ns filters, etc. --- README.md | 1 + src/taoensso/timbre.cljx | 34 +++++++++++++++++++++------------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 292da3c..e23cd71 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,7 @@ This is the biggest win over Java logging IMO. **All** of Timbre's behaviour is :timestamp-opts ; Optional override for inherited {:pattern _ :locale _ :timezone _} :ns-whitelist ; Optional, stacks with active config's whitelist :ns-blacklist ; Optional, stacks with active config's blacklist + :middleware-fn ; Optional, stacks with active config's middleware :fn ; (fn [data]) -> side effects, with keys described below An appender's fn takes a single data map with keys: diff --git a/src/taoensso/timbre.cljx b/src/taoensso/timbre.cljx index b889af9..61f14f4 100644 --- a/src/taoensso/timbre.cljx +++ b/src/taoensso/timbre.cljx @@ -69,6 +69,7 @@ :timestamp-opts ; Optional override for inherited {:pattern _ :locale _ :timezone _} :ns-whitelist ; Optional, stacks with active config's whitelist :ns-blacklist ; Optional, stacks with active config's blacklist + :middleware-fn ; Optional, stacks with active config's middleware :fn ; (fn [data]) -> side effects, with keys described below An appender's fn takes a single data map with keys: @@ -512,26 +513,33 @@ #+clj (assoc data :timestamp_ timestamp_) #+cljs data)) - data ; Final data prep before going to appender + data (conj data {:appender-id id :appender appender :output-fn output-fn :output_ output_ - #+clj :timestamp_ #+clj timestamp_})] + #+clj :timestamp_ #+clj timestamp_}) - ;; NB Unless `async?`, we currently allow appenders to - ;; throw since it's not particularly obvious how/where - ;; we should report problems. Throwing early seems - ;; preferable to just silently dropping errors. In - ;; effect, we currently require appenders to take - ;; responsibility over appropriate trapping. + ?data ; Final data prep before going to appender + (if-let [mfn (:middleware-fn appender)] + (mfn data) + data)] - #+cljs (apfn data) - #+clj - (if async? - (send-off (get-agent id) (fn [_] (apfn data))) - (apfn data)))))))) + (when-let [data ?data] ; Not filtered by middleware + + ;; NB Unless `async?`, we currently allow appenders + ;; to throw since it's not particularly obvious + ;; how/where we should report problems. Throwing + ;; early seems preferable to just silently dropping + ;; errors. In effect, we currently require appenders + ;; to take responsibility over appropriate trapping. + + #+cljs (apfn data) + #+clj + (if async? + (send-off (get-agent id) (fn [_] (apfn data))) + (apfn data))))))))) nil (:appenders config)))))) nil))