Misc utils useful for Telemere handlers, middleware, etc.
(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.
(file-writer {:keys [file append?] :or {append? true}})Experimental, subject to change. Opens the specified file and returns a stateful fn of 2 arities: [content] => Writes given content to file, or no-ops if closed. [] => Closes the writer. Useful for basic handlers that write to a file, etc. Notes: - Automatically creates file and parent dirs as necessary. - Writer should be manually closed after use (with zero-arity call). - Flushes after every write. - Thread safe, locks on single file stream.
(format-error-fn)(format-error-fn {:as _opts})Experimental, subject to change. Returns a (fn format [error]) that: - Takes a platform error (`Throwable` or `js/Error`). - Returns a human-readable error string.
(format-inst-fn)(format-inst-fn {:keys [formatter zone]
:or {formatter java.time.format.DateTimeFormatter/ISO_INSTANT
zone java.time.ZoneOffset/UTC}})(format-inst-fn)(format-inst-fn {:keys [formatter]})Experimental, subject to change without notice.
Returns a (fn format [instant]) that:
- Takes a platform instant (`java.time.Instant` or `js/Date`).
- Returns a formatted human-readable instant string.
Options:
`:zone` (Clj only) `java.time.ZoneOffset` (defaults to UTC).
`:formatter`
`java.time.format.DateTimeFormatter` (Clj) or
`goog.i18n.DateTimeFormat` (Cljs),
Defaults to `ISO8601` formatter (`YYYY-MM-DDTHH:mm:ss.sssZ`),
e.g.: "2011-12-03T10:15:130Z".(format-nsecs-fn)(format-nsecs-fn {:as _opts})Experimental, subject to change.
Returns a (fn format [nanosecs]) that:
- Takes a long nanoseconds (e.g. runtime).
- Returns a human-readable string like:
"1.00m", "4.20s", "340ms", "822μs", etc.(format-signal-fn)(format-signal-fn {:keys [incl-newline? preamble-fn content-fn]
:or {incl-newline? true
preamble-fn (signal-preamble-fn)
content-fn (signal-content-fn)}})Experimental, subject to change. Returns a (fn format [signal]) that: - Takes a Telemere signal (map). - Returns a human-readable signal string. Options: `:incl-newline?` - Include terminating system newline? (default true) `:preamble-fn` - (fn [signal]) => signal preamble string. `:content-fn` - (fn [signal]) => signal content string. See also `pr-signal-fn` for machine-readable output.
(host-ip)(host-ip timeout-msecs timeout-val)Returns cached local host IP address string, or `timeout-val` (default "UnknownHost").
(hostname)(hostname timeout-msecs timeout-val)Returns cached local hostname string, or `timeout-val` (default "UnknownHost").
(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!
(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 user-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 :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 ...})
See also `format-signal-fn` for human-readable output.(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 :thread}
`:format-nsecs-fn` - (fn [nanosecs]) => string.
`:format-error-fn` - (fn [error]) => string.(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.(tcp-socket-writer {:keys [host port ssl? connect-timeout-msecs socket-fn
ssl-socket-fn]
:as opts
:or {connect-timeout-msecs 3000
socket-fn default-socket-fn
ssl-socket-fn default-ssl-socket-fn}})Experimental, subject to change.
Connects to specified TCP socket and returns a stateful fn of 2 arities:
[content] => Writes given content to socket, or no-ops if closed.
[] => Closes the writer.
Useful for basic handlers that write to a TCP socket, etc.
Options:
`:ssl?` - Use SSL/TLS?
`:connect-timeout-msecs` - Connection timeout (default 3000 msecs)
`:socket-fn` - (fn [host port timeout]) => `java.net.Socket`
`:ssl-socket-fn` - (fn [socket host port]) => `java.net.Socket`
Notes:
- Writer should be manually closed after use (with zero-arity call).
- Flushes after every write.
- Will retry failed writes once, then drop.
- Thread safe, locks on single socket stream.
- Advanced users may want a custom implementation using a connection
pool and/or more sophisticated retry semantics, etc.cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |