taoensso.telemere.utils
Misc utils useful for Telemere handlers, middleware, etc.
error-in-signal->maps
clj
(error-in-signal->maps signal)
Experimental, subject to change.
Returns given signal with possible `:error` replaced by
[{:keys [type msg data]} ...] cause chain.
Useful when serializing signals to edn/JSON/etc.error-signal?
clj
(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
clj
(file-writer file append?)
Experimental, subject to change. Feedback welcome!
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.
host-ip
clj
(host-ip timeout-msecs timeout-val)(host-ip)
Returns cached local host IP address string, or `timeout-val` (default "UnknownHost").
hostname
clj
(hostname timeout-msecs timeout-val)(hostname)
Returns cached local hostname string, or `timeout-val` (default "UnknownHost").
minify-signal
clj
(minify-signal signal)
Experimental, subject to change.
Returns minimal signal map, removing:
- Keys with nil values, and
- Keys with redundant values (`:kvs`, `:location`, `:file`).
Useful when serializing signals to edn/JSON/etc.
newline
clj
added in Encore v3.68.0 (2023-09-25)
pr-edn
clj
(pr-edn x)
Prints given arg to an edn string readable with `read-edn`.
pr-signal-fn
clj
(pr-signal-fn pr-fn)(pr-signal-fn pr-fn {:keys [incl-thread? incl-kvs? incl-newline? prep-fn], :or {incl-newline? true, prep-fn (comp error-in-signal->maps minify-signal)}})
Experimental, subject to change.
Returns a (fn pr-signal [signal]) that:
- Takes a Telemere signal.
- Returns machine-readable serialized string of the (minified) signal.
Options include:
`pr-fn` ∈ #{<unary-fn> :edn :json (Cljs only)}
See arglists for more.
Examples:
(pr-signal-fn :edn {<opts>})
(pr-signal-fn :json {<opts>}) ; Cljs only
;; To output JSON for Clj, you must provide an appropriate `pr-fn`.
;; `jsonista` is a good option, Ref. <https://github.com/metosin/jsonista>:
(require '[jsonista.core :as jsonista])
(pr-signal-fn jsonista/write-value-as-string {<opts>})
See also `format-signal-fn` for human-readable output.remove-kvs
clj
(remove-kvs signal)
Returns the given signal without user-level kvs.
signal-content-fn
clj
(signal-content-fn)(signal-content-fn {:keys [incl-thread? incl-kvs? raw-error? 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.
- Returns a signal content ?string (incl. data, ctx, etc.)
See arglists for options.
signal-preamble-fn
clj
(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.
- Returns a signal preamble ?string like:
"2024-03-26T11:14:51.806Z INFO EVENT Hostname taoensso.telemere(2,21) ::ev-id - msg"
See arglists for options.tcp-socket-writer
clj
(tcp-socket-writer host port {:keys [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. Feedback welcome!
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.thread-id
clj
(thread-id)
Returns long id of current thread.
thread-info
clj
(thread-info)
Returns {:keys [group name id]} for current thread.
thread-name
clj
(thread-name)
Returns string name of current thread.