From 207a39865250c77713d5ffad4ce6fcddf7fcfd59 Mon Sep 17 00:00:00 2001 From: Peter Taoussanis Date: Wed, 20 Sep 2023 18:52:20 +0200 Subject: [PATCH] [new] Add `log!` single (map) arity --- src/taoensso/timbre.cljc | 63 +++++++++++++++++++-------------- test/taoensso/timbre_tests.cljc | 4 +-- 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/src/taoensso/timbre.cljc b/src/taoensso/timbre.cljc index 7f465809..4ae36a9e 100644 --- a/src/taoensso/timbre.cljc +++ b/src/taoensso/timbre.cljc @@ -806,32 +806,43 @@ (defn log-wrapper-fn [& args] (timbre/log! :info :p args)) (defmacro log-wrapper-macro [& args] (timbre/keep-callsite `(timbre/log! :info :p ~args)))" - [level msg-type args & [opts]] - (have [:or nil? sequential? symbol?] args) - (let [loc (or (get opts :loc) (enc/get-source &form &env)) - {:keys [ns file line column]} loc] - - ;; level, ns may/not be compile-time consts - (when-not #?(:clj (-elide? level ns) :cljs false) - (let [{:keys [config ?err ?base-data spying?] - :or {config `*config* - ?err :auto}} opts - - callsite-id (callsite-counter) - ns (or (:?ns-str opts) ns) - file (or (:?file opts) file) - line (or (:?line opts) line) - column (or (:?column opts) column) - - vargs-form - (get opts :vargs ; For max flexibility - (if (symbol? args) - `(enc/ensure-vec ~args) - `[ ~@args]))] - - ;; Note pre-resolved expansion - `(taoensso.timbre/-log! ~config ~level ~ns ~file ~line ~column ~msg-type ~?err - (delay ~vargs-form) ~?base-data ~callsite-id ~spying?)))))) + ([{:as opts + :keys [loc level msg-type args vargs + config ?err ?base-data spying?] + :or + {config `*config* + ?err :auto}}] + + (have [:or nil? sequential? symbol?] args) + (let [callsite-id (callsite-counter) + loc (or loc (enc/get-source &form &env)) + {:keys [ns file line column]} loc + + ns (or (get opts :?ns-str) ns) + file (or (get opts :?file) file) + line (or (get opts :?line) line) + column (or (get opts :?column) column) + + elide? (and (enc/const-forms? level ns) (-elide? level ns))] + + (when-not elide? + (let [vargs-form + (or vargs + (if (symbol? args) + `(enc/ensure-vec ~args) + `[ ~@args]))] + + ;; Note pre-resolved expansion + `(taoensso.timbre/-log! ~config ~level ~ns ~file ~line ~column ~msg-type ~?err + (delay ~vargs-form) ~?base-data ~callsite-id ~spying?))))) + + ([level msg-type args & [opts]] + (let [loc (enc/get-source &form &env) + opts (assoc (conj {:loc loc} opts) + :level level, :msg-type msg-type, :args args)] + `(log! ~opts))))) + +(macroexpand '(log! :info :p ["foo"] {:loc {:ns "foo"}})) (comment (do (log! :info :p ["foo"])) diff --git a/test/taoensso/timbre_tests.cljc b/test/taoensso/timbre_tests.cljc index 024abf39..6a7459af 100644 --- a/test/taoensso/timbre_tests.cljc +++ b/test/taoensso/timbre_tests.cljc @@ -35,10 +35,10 @@ (conj timbre/default-config ~m-config {:appenders {:test-appender appender#}})] - (timbre/log! ~level :p ~args {:loc {:ns ~(str ns)}}) + (timbre/log! ~level :p ~args {:loc {:ns ~ns}}) (deref (:data_ appender#))))) -(comment (log-data *ns* :info {:min-level :trace} {} ["x"])) +(comment (macroexpand '(log-data "my-ns" :info {:min-level :trace} {} ["x"]))) ;;;; Core