Skip to content

Commit 2a6089e

Browse files
committed
[mod] Simplify provided handlers, harmonize output
1 parent ca23844 commit 2a6089e

File tree

3 files changed

+68
-77
lines changed

3 files changed

+68
-77
lines changed

src/taoensso/tufte.cljc

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -648,16 +648,19 @@
648648
(s+spc hostname)))
649649

650650
(when ns (s+spc (sigs/format-callsite ns (get signal :coords))))
651-
(when id (s+spc (sigs/format-id ns id)))
651+
652+
(do (enc/sb-append sb " Tufte pstats"))
653+
(when id (enc/sb-append sb " " (sigs/format-id ns id)))
654+
655+
(when-let [ff format-pstats-fn]
656+
(when-let [fs (ff pstats (conj {:incl-newline? false} format-pstats-opts))]
657+
(enc/sb-append sb nl fs)))
652658

653659
#?(:clj (when (enc/and? host incl-host?) (enc/sb-append sb nl " host: " (enc/pr-edn* host))))
654660
#?(:clj (when (enc/and? thread incl-thread?) (enc/sb-append sb nl " thread: " (enc/pr-edn* thread))))
655661
(when-let [data (enc/not-empty-coll data)] (enc/sb-append sb nl " data: " (enc/pr-edn* data)))
656662
(when-let [ctx (enc/not-empty-coll ctx)] (enc/sb-append sb nl " ctx: " (enc/pr-edn* ctx)))
657663

658-
(enc/when-let [ff format-pstats-fn, formatted (ff pstats format-pstats-opts)]
659-
(enc/sb-append sb nl "<<< pstats <<<" nl formatted ">>> pstats >>>"))
660-
661664
(when incl-newline? (enc/sb-append sb nl))
662665
(str sb))))))
663666

@@ -733,7 +736,9 @@
733736
(let [logger (js-console-logger (get signal :level))]
734737
(.call logger logger (str output)))))))))))
735738

736-
(comment ((handler:console) (assoc (dummy-signal) :data {:k1 :v1})))
739+
(comment
740+
((handler:console) (dummy-signal))
741+
((handler:console) (assoc (dummy-signal) :data {:k1 :v1}, :ctx {:k1 :v1})))
737742

738743
(defn handler:accumulating
739744
"Alpha, subject to change.

src/taoensso/tufte/telemere.cljc

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,39 @@
99
[taoensso.telemere :as tel]))
1010

1111
(defn handler:telemere
12-
"Alpha, subject to change.
12+
"Alpha, subject to change!
1313
Returns a signal handler that:
1414
- Takes a Tufte profiling signal (map).
1515
- Creates a corresponding Telemere signal for handling by Telemere.
1616
17-
The Telemere signal will include:
18-
As in Tufte signal: #{:inst :ns :coords :id :level :data :host :thread}
19-
And:
20-
`:kind` --------- `:tufte`
21-
`:msg_` --------- Delay of `pstats` formatted as string table
22-
`:ctx` ---------- (merge `telemere/*ctx*` `tufte/*ctx*`)
23-
`:tufte-signal` - Original Tufte signal (map)
24-
2517
Options:
26-
`:telemere-level-fn` -- (fn [tufte-level]) => telemere-level (default to constantly `:info`)
18+
`:level-fn` ----------- (fn [tufte-level]) => telemere-level (default to `identity`)
2719
`:format-pstats-opts` - Opts map provided to `format-pstats` (default nil)"
2820

2921
([] (handler:telemere nil))
30-
([{:keys [telemere-level-fn format-pstats-opts incl-keys]
31-
:or {telemere-level-fn (fn [tufte-level] tufte-level)}}]
32-
33-
(let []
34-
(fn a-handler:telemere [signal]
35-
(when-let [{:keys [inst ns coords, id level, #?@(:clj [host thread]), ctx data,
36-
pstats format-pstats-fn]} signal]
37-
38-
(taoensso.telemere/signal!
39-
{:inst inst
40-
:ns ns
41-
:coords coords
42-
:kind :tufte
43-
:id id
44-
:level (telemere-level-fn level)
45-
:data data
46-
:tufte-signal signal ; Won't be printed by default (good)
47-
:msg (when-let [ff format-pstats-fn] (delay (ff pstats format-pstats-opts)))
48-
:ctx+
49-
(fn [old]
50-
(if (or (nil? old) (map? old))
51-
(enc/merge-nx old ctx)
52-
(do old)))
53-
54-
#?@(:clj
55-
[:host host
56-
:thread thread])}))))))
57-
58-
(comment ((handler:telemere) (#'tufte/dummy-signal)))
22+
([{:keys [level-fn format-pstats-opts]
23+
:or {level-fn identity}}]
24+
25+
(fn a-handler:telemere [signal]
26+
(when-let [{:keys [inst ns coords, id level, #?@(:clj [host thread]), ctx data,
27+
pstats format-pstats-fn]} signal]
28+
(tel/signal!
29+
{:inst inst
30+
:ns ns
31+
:coords coords
32+
:kind :tufte
33+
:id id
34+
:level (level-fn level)
35+
:ctx+ ctx
36+
:data data
37+
:tufte/pstats pstats ; -> kvs
38+
:msg
39+
(when-let [ff format-pstats-fn]
40+
(when-let [fs (ff pstats (conj {:incl-newline? false} format-pstats-opts))]
41+
(str enc/newline fs)))
42+
43+
#?@(:clj [:host host, :thread thread])})))))
44+
45+
(comment
46+
((handler:telemere) (#'tufte/dummy-signal))
47+
((handler:telemere) (assoc (#'tufte/dummy-signal) :data {:k1 :v1} :ctx {:k1 :v1})))

src/taoensso/tufte/timbre.cljc

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,62 +9,59 @@
99
[taoensso.timbre :as timbre]))
1010

1111
(defn handler:timbre
12-
"Alpha, subject to change.
12+
"Alpha, subject to change!
1313
Returns a signal handler that:
1414
- Takes a Tufte profiling signal (map).
1515
- Uses Timbre to log the signal as a human-readable string.
1616
1717
Options:
18-
`:timbre-level-fn` ---- (fn [tufte-level]) => timbre-level (default to constantly `:info`)
19-
`:format-pstats-opts` - Opts map provided to `format-pstats` (default nil)
20-
`:incl-keys` ---------- Subset of profiling signal keys to retain from those
21-
otherwise excluded by default: #{:host :thread}"
18+
`:level-fn` ----------- (fn [tufte-level]) => timbre-level (default `identity`).
19+
`:format-pstats-opts` - Opts map provided to `format-pstats` (default nil)"
2220

2321
([] (handler:timbre nil))
24-
([{:keys [timbre-level-fn format-pstats-opts incl-keys]
25-
:or {timbre-level-fn (fn [tufte-level] :info)}}]
22+
([{:keys [level-fn format-pstats-opts]
23+
:or {level-fn identity}}]
2624

27-
(let [nl enc/newline
28-
incl-host? (contains? incl-keys :host)
29-
incl-thread? (contains? incl-keys :thread)]
25+
(fn a-handler:timbre [signal]
26+
(let [{:keys [inst ns coords, id level, #?@(:clj [host thread]), ctx data,
27+
pstats format-pstats-fn]} signal]
3028

31-
(fn a-handler:timbre [signal]
32-
(let [{:keys [inst ns coords, id level, #?@(:clj [host thread]), ctx data,
33-
pstats format-pstats-fn]} signal]
34-
(timbre/log!
35-
{:loc (when ns (let [[line column] coords] {:ns ns, :line line, :column column}))
36-
:level (timbre-level-fn level)
37-
:instant #?(:clj (enc/as-dt inst), :cljs inst)
38-
:msg-type :p
39-
:vargs
40-
(let [sb (enc/str-builder)
41-
s+spc (enc/sb-appender sb " ")]
29+
(timbre/log!
30+
{:loc {:ns ns, :line (get coords 0)}
31+
:level (level-fn level)
32+
:instant #?(:clj (enc/as-dt inst), :cljs inst)
33+
:msg-type :p
34+
:tufte/pstats pstats ; -> log data map
35+
:vargs
36+
(let [nl enc/newline
37+
sb (enc/str-builder)]
4238

43-
(s+spc "Tufte signal")
44-
(when id (s+spc (sigs/format-id ns id)))
39+
(do (enc/sb-append sb "Tufte pstats"))
40+
(when id (enc/sb-append sb " " (sigs/format-id ns id)))
4541

46-
#?(:clj (when (enc/and? host incl-host?) (enc/sb-append sb nl " host: " (enc/pr-edn* host))))
47-
#?(:clj (when (enc/and? thread incl-thread?) (enc/sb-append sb nl " thread: " (enc/pr-edn* thread))))
48-
(when-let [data (enc/not-empty-coll data)] (enc/sb-append sb nl " data: " (enc/pr-edn* data)))
49-
(when-let [ctx (enc/not-empty-coll ctx)] (enc/sb-append sb nl " ctx: " (enc/pr-edn* ctx)))
42+
(when-let [ff format-pstats-fn]
43+
(when-let [fs (ff pstats (conj {:incl-newline? false} format-pstats-opts))]
44+
(enc/sb-append sb nl fs)))
5045

51-
(enc/when-let [ff format-pstats-fn, formatted (ff pstats format-pstats-opts)]
52-
(enc/sb-append sb nl "<<< pstats <<<" nl formatted ">>> pstats >>>"))
46+
(when-let [data (enc/not-empty-coll data)] (enc/sb-append sb nl " data: " (enc/pr-edn* data)))
47+
(when-let [ctx (enc/not-empty-coll ctx)] (enc/sb-append sb nl " ctx: " (enc/pr-edn* ctx)))
5348

54-
[(str sb)])}))))))
49+
[(str sb)])})))))
5550

56-
(comment ((handler:timbre) (assoc (#'tufte/dummy-signal) :data {:k1 :v1})))
51+
(comment
52+
((handler:timbre) (#'tufte/dummy-signal))
53+
((handler:timbre) (assoc (#'tufte/dummy-signal) :data {:k1 :v1} :ctx {:k1 :v1})))
5754

5855
(enc/deprecated
5956
(defn ^:no-doc ^:deprecated add-timbre-logging-handler!
6057
"Prefer (add-handler! <handler-id> (handler:timbre) <dispatch-opts>)."
6158
[{:keys [ns-pattern handler-id timbre-level]
62-
:or {ns-pattern "*"
59+
:or {ns-pattern "*"
6360
handler-id :timbre
6461
timbre-level :info}}]
6562

6663
(let [timbre-level-fn (if (fn? timbre-level) timbre-level (fn [_] timbre-level))
67-
handler-fn (handler:timbre {:timbre-level-fn timbre-level-fn})
64+
handler-fn (handler:timbre {:level-fn timbre-level-fn})
6865
dispatch-opts
6966
(when (and ns-pattern (not= ns-pattern "*"))
7067
{:ns-filter ns-pattern})]

0 commit comments

Comments
 (0)