Skip to content

Instantly share code, notes, and snippets.

@stathissideris
Created February 11, 2018 09:26
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 stathissideris/975ee28b4d12b6eb07722c865caa3160 to your computer and use it in GitHub Desktop.
Save stathissideris/975ee28b4d12b6eb07722c865caa3160 to your computer and use it in GitHub Desktop.
Simple pedestal interceptors to log how the context changes as it gets passed around in the chain
(require '[io.pedestal.log :as log]
'[clojure.data :as diff]
'[io.pedestal.interceptor.chain :as chain])
(defn log-diffs [previous current]
(let [[deleted added] (diff/diff (dissoc previous ::chain/queue ::chain/stack ::previous-ctx)
(dissoc current ::chain/queue ::chain/stack ::previous-ctx))]
(when deleted (log/debug :deleted deleted))
(when added (log/debug :added added))))
(defn handle-debug [ctx]
(log-diffs (::previous-ctx ctx) ctx)
(assoc ctx ::previous-ctx ctx))
(def debug
{:name :debug
:enter handle-debug
:leave handle-debug})
;;use this as your first interceptor
(def inject-debug
{:name :inject-debug
:enter
(fn [ctx]
(assoc
ctx
::previous-ctx
ctx
::chain/queue
(into clojure.lang.PersistentQueue/EMPTY
(cons debug
(-> ctx
::chain/queue
seq
(interleave (repeat debug)))))))})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment