Skip to content

Commit

Permalink
[new] Add log! single (map) arity
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaoussanis committed Sep 27, 2023
1 parent 35918e1 commit 207a398
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
63 changes: 37 additions & 26 deletions src/taoensso/timbre.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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"]))
Expand Down
4 changes: 2 additions & 2 deletions test/taoensso/timbre_tests.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 207a398

Please sign in to comment.