Created
February 11, 2018 09:26
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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