[#15]: adding logging AOP for REPL examples
This commit is contained in:
parent
41d4c130b3
commit
2d1f77df92
3 changed files with 53 additions and 0 deletions
|
|
@ -17,6 +17,7 @@
|
|||
[check.start-with-test]
|
||||
[check.suspend-resume-test]
|
||||
[mount.core :as mount]
|
||||
[app.utils.logging :refer [with-logging-status]]
|
||||
[app :refer [create-nyse-schema find-orders add-order]])) ;; <<<< replace this your "app" namespace(s) you want to be available at REPL time
|
||||
|
||||
(defn start []
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@
|
|||
:dependencies [[yesql "0.5.1"]
|
||||
[ch.qos.logback/logback-classic "1.1.3"]
|
||||
[org.clojure/tools.logging "0.3.1"]
|
||||
[robert/hooke "1.3.0"]
|
||||
[org.clojure/tools.namespace "0.2.11"]
|
||||
[org.clojure/tools.nrepl "0.2.11"]
|
||||
[com.datomic/datomic-free "0.9.5327" :exclusions [joda-time]]]}})
|
||||
|
|
|
|||
51
test/app/utils/logging.clj
Normal file
51
test/app/utils/logging.clj
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
(ns app.utils.logging
|
||||
(:require [robert.hooke :refer [add-hook clear-hooks]]
|
||||
[clojure.string :refer [split]]
|
||||
[clojure.tools.logging :refer [info]]))
|
||||
|
||||
(alter-meta! *ns* assoc ::load false)
|
||||
|
||||
(defn- f-to-action [f]
|
||||
(let [fname (-> (str f)
|
||||
(split #"@")
|
||||
first)]
|
||||
(case fname
|
||||
"mount.core$up" :up
|
||||
"mount.core$down" :down
|
||||
"mount.core$sigstop" :suspend
|
||||
"mount.core$sigcont" :resume
|
||||
:noop)))
|
||||
|
||||
(defn whatcha-doing? [{:keys [started? suspended? suspend]} action]
|
||||
(case action
|
||||
:up (if suspended? "resuming"
|
||||
(if-not started? "starting"))
|
||||
:down (if (or started? suspended?) "stopping")
|
||||
:suspend (if (and started? suspend) "suspending")
|
||||
:resume (if suspended? "resuming")))
|
||||
|
||||
(defn log-status [f & args]
|
||||
(let [{:keys [ns name] :as state} (second args)
|
||||
action (f-to-action f)]
|
||||
(when-let [taking-over-the-world (whatcha-doing? state action)]
|
||||
(info (str ">> " taking-over-the-world ".. " (ns-resolve ns name))))
|
||||
(apply f args)))
|
||||
|
||||
(defonce lifecycle-fns
|
||||
#{#'mount.core/up
|
||||
#'mount.core/down
|
||||
#'mount.core/sigstop
|
||||
#'mount.core/sigcont})
|
||||
|
||||
(defn with-logging-status []
|
||||
(doall (map #(add-hook % log-status) lifecycle-fns)))
|
||||
|
||||
(defn without-logging-status []
|
||||
(doall (map #(clear-hooks %) lifecycle-fns)))
|
||||
|
||||
|
||||
;; here is just to illustrate lificycle of states in REPL
|
||||
;; if needed in reality will be called in "-main" or another entry point
|
||||
|
||||
(without-logging-status)
|
||||
(with-logging-status)
|
||||
Loading…
Reference in a new issue