mirror of https://github.com/status-im/timbre.git
[#207] Fix: middleware couldn't influence automatic msg_ generation
As of this commit: The values of :msg_ and :output_ will depend on the actual value of :vargs *at the time of first evaluation*.
This commit is contained in:
parent
d6181ef1b0
commit
e4f33c5341
|
@ -293,52 +293,41 @@
|
||||||
|
|
||||||
[context & body] `(binding [*context* ~context] ~@body))
|
[context & body] `(binding [*context* ~context] ~@body))
|
||||||
|
|
||||||
(defn- next-vargs [v] (if (> (count v) 1) (subvec v 1) []))
|
(defn- vrest [v] (if (> (count v) 1) (subvec v 1) []))
|
||||||
(defn- vargs->margs
|
(defn- parse-vargs
|
||||||
"Transforms raw vargs -> {:?err _ :?meta _ ...}, extracting:
|
"vargs -> [?err ?meta ?msg-fmt api-vargs]"
|
||||||
* Special error or ^:meta {} (experimental, undocumented) v0
|
|
||||||
* Message format string
|
|
||||||
* Message string delay"
|
|
||||||
[?err msg-type vargs]
|
[?err msg-type vargs]
|
||||||
(let [auto-error? (enc/kw-identical? ?err :auto)
|
(let [auto-error? (enc/kw-identical? ?err :auto)
|
||||||
msg-fmt? (enc/kw-identical? msg-type :f)
|
fmt-msg? (enc/kw-identical? msg-type :f)
|
||||||
[v0] vargs]
|
[v0] vargs]
|
||||||
|
|
||||||
(if (and auto-error? (enc/error? v0))
|
(if (and auto-error? (enc/error? v0))
|
||||||
(let [vargs (next-vargs vargs)
|
(let [?err v0
|
||||||
?msg-fmt (if msg-fmt? (let [[v0] vargs] v0) nil)
|
?meta nil
|
||||||
vargs (if msg-fmt? (next-vargs vargs) vargs)
|
vargs (vrest vargs)
|
||||||
msg_ (delay
|
?msg-fmt (if fmt-msg? (let [[v0] vargs] v0) nil)
|
||||||
(case msg-type
|
vargs (if fmt-msg? (vrest vargs) vargs)]
|
||||||
nil ""
|
|
||||||
:p (str-join vargs)
|
|
||||||
:f (enc/format* (have string? ?msg-fmt) vargs)))]
|
|
||||||
|
|
||||||
{:?err v0 :?meta nil :?msg-fmt ?msg-fmt :msg_ msg_ :vargs vargs})
|
[?err ?meta ?msg-fmt vargs])
|
||||||
|
|
||||||
(let [?meta (if (and (map? v0) (:meta (meta v0))) v0 nil)
|
(let [?meta (if (and (map? v0) (:meta (meta v0))) v0 nil)
|
||||||
?err (or (:err ?meta) (if auto-error? nil ?err))
|
?err (or (:err ?meta) (if auto-error? nil ?err))
|
||||||
?meta (dissoc ?meta :err)
|
?meta (dissoc ?meta :err)
|
||||||
vargs (if ?meta (next-vargs vargs) vargs)
|
vargs (if ?meta (vrest vargs) vargs)
|
||||||
?msg-fmt (if msg-fmt? (let [[v0] vargs] v0) nil)
|
?msg-fmt (if fmt-msg? (let [[v0] vargs] v0) nil)
|
||||||
vargs (if msg-fmt? (next-vargs vargs) vargs)
|
vargs (if fmt-msg? (vrest vargs) vargs)]
|
||||||
msg_ (delay
|
|
||||||
(case msg-type
|
|
||||||
nil ""
|
|
||||||
:p (str-join vargs)
|
|
||||||
:f (enc/format* (have string? ?msg-fmt) vargs)))]
|
|
||||||
|
|
||||||
{:?err ?err :?meta ?meta :?msg-fmt ?msg-fmt :msg_ msg_ :vargs vargs}))))
|
[?err ?meta ?msg-fmt vargs]))))
|
||||||
|
|
||||||
(comment
|
(comment
|
||||||
(let [ex (Exception. "ex")]
|
(let [ex (Exception. "ex")]
|
||||||
(qb 10000
|
(qb 10000
|
||||||
(vargs->margs :auto :f ["fmt" :a :b :c])
|
(parse-vargs :auto :f ["fmt" :a :b :c])
|
||||||
(vargs->margs :auto :p [ex :a :b :c])
|
(parse-vargs :auto :p [ex :a :b :c])
|
||||||
(vargs->margs :auto :p [^:meta {:foo :bar} :a :b :c])
|
(parse-vargs :auto :p [^:meta {:foo :bar} :a :b :c])
|
||||||
(vargs->margs :auto :p [ {:foo :bar} :a :b :c])
|
(parse-vargs :auto :p [ {:foo :bar} :a :b :c])
|
||||||
(vargs->margs :auto :p [ex])
|
(parse-vargs :auto :p [ex])
|
||||||
(vargs->margs :auto :p [^:meta {:err ex} :a :b :c])))
|
(parse-vargs :auto :p [^:meta {:err ex} :a :b :c])))
|
||||||
;; [2.79 2.51 6.13 1.65 1.94 6.2]
|
;; [2.79 2.51 6.13 1.65 1.94 6.2]
|
||||||
(infof "Hi %s" "steve")
|
(infof "Hi %s" "steve")
|
||||||
(infof ^:meta {:hash :bar} "Hi %s" "steve")
|
(infof ^:meta {:hash :bar} "Hi %s" "steve")
|
||||||
|
@ -359,46 +348,56 @@
|
||||||
(when (may-log? level ?ns-str config)
|
(when (may-log? level ?ns-str config)
|
||||||
(let [instant (enc/now-dt)
|
(let [instant (enc/now-dt)
|
||||||
context *context*
|
context *context*
|
||||||
vargs @vargs_
|
vargs1 @vargs_
|
||||||
|
|
||||||
;; {:keys [?err ?meta ?msg-fmt msg_ vargs]}:
|
[?err ?meta ?msg-fmt vargs1] (parse-vargs ?err msg-type vargs1)
|
||||||
margs (vargs->margs ?err msg-type vargs)
|
|
||||||
data
|
|
||||||
(merge
|
|
||||||
?base-data
|
|
||||||
margs
|
|
||||||
{:instant instant
|
|
||||||
:level level
|
|
||||||
:context context
|
|
||||||
:config config ; Entire config!
|
|
||||||
:?ns-str ?ns-str
|
|
||||||
:?file ?file
|
|
||||||
:?line ?line
|
|
||||||
#+clj :hostname_ #+clj (delay (get-hostname))
|
|
||||||
:error-level? (#{:error :fatal} level)
|
|
||||||
|
|
||||||
;; Uniquely identifies a particular logging call for purposes of
|
final-vargs_ (atom vargs1) ; Allow middleware to influece msg_
|
||||||
;; rate limiting, etc.
|
msg_
|
||||||
:hash_ ; TODO Undocumented (experimental)
|
(case msg-type
|
||||||
(delay
|
nil ""
|
||||||
(hash
|
:p (delay (str-join @final-vargs_))
|
||||||
|
:f (do
|
||||||
|
(have? string? ?msg-fmt)
|
||||||
|
(delay (enc/format* @final-vargs_))))
|
||||||
|
|
||||||
|
;; Uniquely identifies a particular logging call for purposes of
|
||||||
|
;; rate limiting, etc.
|
||||||
|
hash_
|
||||||
|
(delay
|
||||||
|
(hash
|
||||||
;; Nb excl. instant
|
;; Nb excl. instant
|
||||||
[callsite-id ; Only useful for direct macro calls
|
[callsite-id ; Only useful for direct macro calls
|
||||||
(:?msg-fmt margs)
|
?msg-fmt
|
||||||
(get-in margs [:?meta :hash] ; Explicit hash provided
|
(get ?meta :hash ; Explicit hash provided
|
||||||
(:vargs margs))]))
|
@final-vargs_)]))
|
||||||
|
|
||||||
;; :?err <from-margs>
|
data ; Pre-middleware
|
||||||
;; :?meta <from-margs> ; TODO Undocumented (experimental)
|
(conj
|
||||||
;; :?msg-fmt <from-margs> ; TODO Undocumented (experimental)
|
(or ?base-data {})
|
||||||
;; :msg_ <from-margs>
|
{:instant instant
|
||||||
;; :vargs <from-margs>
|
:level level
|
||||||
|
:context context
|
||||||
|
:config config ; Entire config!
|
||||||
|
:?ns-str ?ns-str
|
||||||
|
:?file ?file
|
||||||
|
:?line ?line
|
||||||
|
#+clj :hostname_ #+clj (delay (get-hostname))
|
||||||
|
:error-level? (#{:error :fatal} level)
|
||||||
|
|
||||||
;;; Deprecated
|
:?err_ (delay ?err) ; Deprecated
|
||||||
:?err_ (delay (:?err margs))
|
:vargs_ (delay @final-vargs_) ; ''
|
||||||
:vargs_ (delay (:vargs margs))})
|
|
||||||
|
|
||||||
?data
|
:?err ?err
|
||||||
|
:?meta ?meta ; TODO Undocumented (experimental)
|
||||||
|
:?msg-fmt ?msg-fmt ; ''
|
||||||
|
:hash_ hash_ ; ''
|
||||||
|
:vargs vargs1
|
||||||
|
:msg_ msg_})
|
||||||
|
|
||||||
|
?middleware (:middleware config)
|
||||||
|
|
||||||
|
?data ; Post middleware
|
||||||
(reduce ; Apply middleware: data->?data
|
(reduce ; Apply middleware: data->?data
|
||||||
(fn [acc mf]
|
(fn [acc mf]
|
||||||
(let [result (mf acc)]
|
(let [result (mf acc)]
|
||||||
|
@ -406,9 +405,10 @@
|
||||||
(reduced nil)
|
(reduced nil)
|
||||||
result)))
|
result)))
|
||||||
data
|
data
|
||||||
(:middleware config))]
|
?middleware)]
|
||||||
|
|
||||||
(when-let [data ?data] ; Not filtered by middleware
|
(when-let [data ?data] ; Not filtered by middleware
|
||||||
|
(reset! final-vargs_ (:vargs data))
|
||||||
(let [;; Optimization: try maximize output+timestamp sharing
|
(let [;; Optimization: try maximize output+timestamp sharing
|
||||||
;; between appenders
|
;; between appenders
|
||||||
output-fn1 (enc/memoize_ (get config :output-fn default-output-fn))
|
output-fn1 (enc/memoize_ (get config :output-fn default-output-fn))
|
||||||
|
|
Loading…
Reference in New Issue