Skip to content

Commit

Permalink
[new] Extend log-data to allow arb forms
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaoussanis committed Aug 29, 2024
1 parent 1a20319 commit 3892691
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions test/taoensso/timbre_tests.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,33 @@

;;;; Utils, etc.

(defn apn
"Returns a test appender."
([ ] (apn nil))
(defn capturing-appender
([ ] (capturing-appender nil))
([m]
(let [data_ (atom nil)]
(let [data_ (volatile! nil)]
(conj
{:enabled? true
:fn (fn [data] (reset! data_ data))
:data_ data_}
(with-meta
{:enabled? true
:fn (fn [data] (vreset! data_ data))}
{:data_ data_})
m))))

(comment (apn {:min-level :info :ns-filter "*"}))

(defmacro log-data
"Executes an easily-configured log call and returns ?data sent to test appender."
[ns level m-config m-appender args]
`(let [appender# (apn ~m-appender)]
(binding [timbre/*config*
(conj timbre/default-config ~m-config
{:appenders {:test-appender appender#}})]

(timbre/log! ~level :p ~args {:loc {:ns ~ns}})
(deref (:data_ appender#)))))
(comment (capturing-appender {:min-level :info :ns-filter "*"}))

#?(:clj
(defmacro log-data
([ns level m-config m-appender args]
`(log-data ~m-config ~m-appender
(timbre/log! ~level :p ~args {:loc {:ns ~ns}})))

([ form] `(log-data nil nil ~form))
([m-config m-appender form]
`(let [appender# (capturing-appender ~m-appender)]
(binding [timbre/*config*
(conj timbre/default-config ~m-config
{:appenders {:capturing-appender appender#}})]
(do ~form))
(update (deref (:data_ (meta appender#))) :msg_ force)))))

(comment (macroexpand '(log-data "my-ns" :info {:min-level :trace} {} ["x"])))

Expand Down

0 comments on commit 3892691

Please sign in to comment.