Skip to content

Instantly share code, notes, and snippets.

@saulshanabrook
Created July 29, 2017 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saulshanabrook/533fce4e1598cc7a81fae594443842c6 to your computer and use it in GitHub Desktop.
Save saulshanabrook/533fce4e1598cc7a81fae594443842c6 to your computer and use it in GitHub Desktop.
abandonded structured logger
;; terms:
;;
;; label: is the name of an event we want to log, i.e. :start, :end, etc
;; input: is the stuff you want to log i.e. {:id 1 :start-time 20}
;; compute-graph: is a graph that that takes in the existing mapping of labels->computed
;; along with the `input`, and returns the new `computed` for the event.
;; computed: is the return value of compute-graph
;; handler: map of label to handle function. Each handle function is called with the `labels->computed`
;; that exist when the that event is called.
(def structured-logger-graph
{:label->compute-fn
(fnk [label->compute-graph wrap-graph]
(map-vals-w-keys
(fn [label compute-graph]
(graph/lazy-compile (wrap-graph [label] compute-graph)))
label->compute-graph))
:handlers
(fnk [name->handler wrap-graph]
(for [[handler-name handler] name->handler]
(map-vals-w-keys
(fn [label f]
(wrap-graph [:handlers handler-name label] f))
handler)))
:label->handle-fns
(fnk [handlers]
(->> handlers
(mapcat identity)
(reduce
(fn [m [label handler-fn]]
(update m label conj handler-fn))
{})))
:log!
(fnk [label->compute-fn label->handle-fns]
(fn [label input]
;; compute
(let [compute-fn (label label->compute-fn)
computed (compute-fn input)
computed-merged (merge computed input)]
;; trigger handlers
(doseq [handle-fn (label label->handle-fns)]
(handle-fn computed-merged))
;; return the computed for this event
computed)))})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment