Back
spy (clj)
(source)macro
(spy form)
(spy level form)
(spy level name form)
(spy config level name form)
Evaluates named form and logs its result. Always returns the result.
Defaults to `:debug` logging level, and unevaluated form as name:
(spy (+ 3 2)) => (timbre/debug '(+ 3 2) "=>" 5)
Examples
fulcrologic/fulcro
(ns com.fulcrologic.fulcro.cards.error-boundary-cards
(:require
[nubank.workspaces.card-types.fulcro3 :as ct.fulcro]
[nubank.workspaces.core :as ws]
[com.fulcrologic.fulcro.application :as app]
[com.fulcrologic.fulcro.components :as comp :refer [defsc]]
[com.fulcrologic.fulcro.dom :as dom]
[com.fulcrologic.fulcro.mutations :as m]
[taoensso.timbre :as log]))
(defsc BadActor [this {:keys [id] :as props}]
{:query [:id]
:ident :id
:componentDidMount (fn [this]
(when (and
(not (comp/get-computed this :reset?))
(= 2 (:id (comp/props this))))
(throw (ex-info "mount Craptastic!" {}))))
:componentWillUnmount (fn [this] (log/spy :info (comp/props this))
(when (= 3 (:id (comp/props this)))
(throw (ex-info "unmount Craptastic!" {}))))
:initial-state {:id :param/id}}
(if (and (= id 4)
(not (comp/get-computed this :reset?)))
(throw (ex-info "Render craptastic!" {}))
(dom/div "Actor")))
(defsc Child [this {:child/keys [id name actor] :as props}]
{:query [:child/id :child/name {:child/actor (comp/get-query BadActor)}]
:ident :child/id
:componentDidCatch (fn [this err info] (log/spy :error [err info]))
:getDerivedStateFromError (fn [error]
(log/spy :info error)
{:error? true})
:initial-state {:child/id :param/id :child/name :param/name
:child/actor {:id :param/id}}}
(if (log/spy :info (comp/get-state this :error?))
(dom/div
"There was an error in this part of the UI."
(dom/button {:onClick #(comp/set-state! this {:error? false})} "Retry"))
(dom/div
(dom/label "Child: " name)
(ui-bad-actor actor {:reset? (contains? (comp/get-state this) :error?)}))))
fulcrologic/statecharts
(ns invocations
(:require
[com.fulcrologic.statecharts :as sc]
[com.fulcrologic.statecharts.algorithms.v20150901-validation :as v]
[com.fulcrologic.statecharts.chart :refer [statechart]]
[com.fulcrologic.statecharts.elements :refer [Send data-model final invoke log on-entry on-exit state transition]]
[com.fulcrologic.statecharts.environment :as e]
[com.fulcrologic.statecharts.event-queue.core-async-event-loop :as loop]
[com.fulcrologic.statecharts.simple :as simple]
[taoensso.timbre :as log]))
(state {:id :X}
(on-entry {}
(log {:label "Child X" :expr (fn [_ {:keys [x]}] x)}))
;; NOTE: The future invocation processor is only coded for CLJ, so this invocation is ignored in CLJS
(invoke {:id :child/future
:type :future
:params {:time (fn [_ {:keys [x]}] x)}
:src (fn [{:keys [time]}]
(log/info "Invocation running for" time)
#?(:clj (try
(Thread/sleep (* 1000 time))
(catch InterruptedException e
(log/info "Future cancelled!")
(throw e))))
(log/info "Future function completed")
{:future-result :DONE})})
(transition {:event :done.invoke}
(log {:label "Future indicated that it completed"
:expr (fn [e d]
(log/spy :debug d))}))
(transition {:event :child/swap :target :Y}))
(state {:id :Y}
(on-entry {}
(log {:label "Child Y"}))
(on-exit {}
(Send {:event :child.left-y
:type ::sc/chart
:targetexpr (fn [env data]
(e/parent-session-id env))}))
(transition {:event :child/swap :target :X})))
(final {:id :state/final})))
fulcrologic/fulcro-rad-demo
(ns com.example.model.item
(:require
[com.fulcrologic.rad.attributes :as attr :refer [defattr]]
[com.fulcrologic.rad.attributes-options :as ao]
[com.wsscode.pathom.connect :as pc]
#?(:clj [com.example.components.database-queries :as queries])
[taoensso.timbre :as log]))
(defattr all-items :item/all-items :ref
{ao/target :item/id
::pc/output [{:item/all-items [:item/id]}]
::pc/resolve (fn [{:keys [query-params] :as env} _]
#?(:clj
{:item/all-items (queries/get-all-items env (log/spy :info query-params))}))})
#?(:clj
(pc/defresolver item-category-resolver [{:keys [parser] :as env} {:item/keys [id]}]
{::pc/input #{:item/id}
::pc/output [:category/id :category/label]}
(let [result (parser env [{[:item/id id] [{:item/category [:category/id :category/label]}]}])]
(get-in (log/spy :info result) [[:item/id id] :item/category]))))
ciste/ciste
(ns ciste.debug
"The ciste.debug namespace contains only a single macro: spy. Spy will
log the code it wraps as well as a pretty-printed version of it's
value. That value will then be returned. This allows you to easily
monitor any bit of code by simply wrapping it with spy."
(:require [clojure.pprint :refer [pprint]]
[taoensso.timbre :as timbre]))
(defmacro spy
"Wrap an expression in this macro to log the expression followed by the
pretty-printed version of the result.
fulcrologic/video-series
(ns app.model.session
(:require
[com.wsscode.pathom.connect :as pc]
[com.fulcrologic.fulcro.server.api-middleware :refer [augment-response]]
[taoensso.timbre :as log]))
(pc/defresolver current-user-resolver [env _]
{::pc/output [{:session/current-user [:user/id]}]}
(let [{:user/keys [id email]} (log/spy :info (-> env :request :session))]
{:session/current-user
(if id
{:user/email email
:user/id id
:user/valid? true}
{:user/id :nobody
:user/valid? false})}))