|
9 | 9 | [taoensso.timbre :as timbre])) |
10 | 10 |
|
11 | 11 | (defn handler:timbre |
12 | | - "Alpha, subject to change. |
| 12 | + "Alpha, subject to change! |
13 | 13 | Returns a signal handler that: |
14 | 14 | - Takes a Tufte profiling signal (map). |
15 | 15 | - Uses Timbre to log the signal as a human-readable string. |
16 | 16 |
|
17 | 17 | 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)" |
22 | 20 |
|
23 | 21 | ([] (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}}] |
26 | 24 |
|
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] |
30 | 28 |
|
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)] |
42 | 38 |
|
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))) |
45 | 41 |
|
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))) |
50 | 45 |
|
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))) |
53 | 48 |
|
54 | | - [(str sb)])})))))) |
| 49 | + [(str sb)])}))))) |
55 | 50 |
|
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}))) |
57 | 54 |
|
58 | 55 | (enc/deprecated |
59 | 56 | (defn ^:no-doc ^:deprecated add-timbre-logging-handler! |
60 | 57 | "Prefer (add-handler! <handler-id> (handler:timbre) <dispatch-opts>)." |
61 | 58 | [{:keys [ns-pattern handler-id timbre-level] |
62 | | - :or {ns-pattern "*" |
| 59 | + :or {ns-pattern "*" |
63 | 60 | handler-id :timbre |
64 | 61 | timbre-level :info}}] |
65 | 62 |
|
66 | 63 | (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}) |
68 | 65 | dispatch-opts |
69 | 66 | (when (and ns-pattern (not= ns-pattern "*")) |
70 | 67 | {:ns-filter ns-pattern})] |
|
0 commit comments