taoensso.telemere.utils
Misc utils useful for Telemere handlers, middleware, etc.
error-signal?
cljs
(error-signal? signal)
Experimental, subject to change.
Returns true iff given signal has an `:error` value, or a `:kind` or `:level`
that indicates that it's an error.
hex-uid-fn
cljs
(hex-uid-fn)(hex-uid-fn {:keys [secure? root-len child-len], :or {root-len 32, child-len 16}})
Experimental, subject to change.
Returns a (fn hex-uid [root?]) that returns a random hex-style uid string like:
"05039666eb9dc3206475f44ab9f3d843" - 128 bit (32 char) root uid
"721fcef639a51513" - 64 bit (16 char) non-root (child) uid
js-console-logger
cljs
(js-console-logger level)
Returns JavaScript console logger to match given signal level:
`:trace` -> `js/console.trace`,
`:error` -> `js/console.error`, etc.
Defaults to `js.console.log` for unmatched signal levels.
NB: assumes that `js/console` exists, handler constructors should check first!
nano-uid-fn
cljs
(nano-uid-fn)(nano-uid-fn {:keys [secure? root-len child-len], :or {root-len 21, child-len 10}})
Experimental, subject to change.
Returns a (fn nano-uid [root?]) that returns a random nano-style uid string like:
"r76-B8LoIPs5lBG1_Uhdy" - 126 bit (21 char) root uid
"tMEYoZH0K-" - 60 bit (10 char) non-root (child) uid
newline
cljs
added in Encore v3.68.0 (2023-09-25)
pr-edn
cljs
(pr-edn x)
Prints given arg to an edn string readable with `read-edn`.
pr-json
cljs
added in Encore v3.98.0 (2024-04-08)
(pr-json x)
Returns given Cljs argument as JSON string.
pr-signal-fn
cljs
(pr-signal-fn)(pr-signal-fn {:keys [pr-fn incl-kvs? incl-nils? incl-newline? incl-keys], :as opts, :or {pr-fn :edn, incl-newline? true}})
Experimental, subject to change.
Returns a (fn pr [signal]) that:
- Takes a Telemere signal (map).
- Returns a machine-readable signal ?string.
Options:
`:pr-fn` - ∈ #{<unary-fn> :edn (default) :json (Cljs only)}
`:incl-kvs?` - Include signal's app-level kvs? (default false)
`:incl-nils?` - Include signal's keys with nil values? (default false)
`:incl-newline?` - Include terminating system newline? (default true)
`:incl-keys` - Subset of signal keys to retain from those otherwise
excluded by default: #{:location :kvs :file :host :thread}
Examples:
(pr-signal-fn {:pr-fn :edn ...}) ; Outputs edn
(pr-signal-fn {:pr-fn :json ...}) ; Outputs JSON (Cljs only)
To output JSON for Clj, you must provide an appropriate `:pr-fn`.
`jsonista` offers one good option, Ref. <https://github.com/metosin/jsonista>:
(require '[jsonista.core :as jsonista])
(pr-signal-fn {:pr-fn jsonista/write-value-as-string ...})
Motivation:
Why use this util instead of just directly using the print function
given to `:pr-fn`? Signals are optimized for cheap creation and easy handling,
so may contain things like nil values and duplicated content.
This util efficiently clean signals of such noise, helping reduce
storage/transmission size, and making key info easier to see.
See also `format-signal-fn` for human-readable output.signal-content-fn
cljs
(signal-content-fn)(signal-content-fn {:keys [raw-error? incl-keys format-nsecs-fn format-error-fn], :or {format-nsecs-fn (format-nsecs-fn), format-error-fn (format-error-fn)}})
Experimental, subject to change.
Returns a (fn content [signal]) that:
- Takes a Telemere signal (map).
- Returns a signal content ?string (incl. data, ctx, etc.).
Options:
`:raw-error?` - Retain unformatted error? (default false)
`:incl-keys` - Subset of signal keys to retain from those
otherwise excluded by default: #{:kvs :host :thread}
`:format-nsecs-fn` - (fn [nanosecs]) => string.
`:format-error-fn` - (fn [error]) => string.signal-preamble-fn
cljs
(signal-preamble-fn)(signal-preamble-fn {:keys [format-inst-fn], :or {format-inst-fn (format-inst-fn)}})
Experimental, subject to change.
Returns a (fn preamble [signal]) that:
- Takes a Telemere signal (map).
- Returns a signal preamble ?string like:
"2024-03-26T11:14:51.806Z INFO EVENT Hostname taoensso.telemere(2,21) ::ev-id - msg"
Options:
`:format-inst-fn` - (fn format [instant]) => string.uuid-str
cljs
(uuid-str max-len)(uuid-str)
Returns a random UUID string of given length (max 36).
Uses strong randomness when possible. See also `uuid`, `nanoid`, `rand-id-fn`.