diff --git a/dev/dev.clj b/dev/dev.clj index cff03fc..ee0b160 100644 --- a/dev/dev.clj +++ b/dev/dev.clj @@ -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 [] diff --git a/project.clj b/project.clj index f5ee1a8..9b7650f 100644 --- a/project.clj +++ b/project.clj @@ -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]]]}}) diff --git a/test/app/utils/logging.clj b/test/app/utils/logging.clj new file mode 100644 index 0000000..db086cb --- /dev/null +++ b/test/app/utils/logging.clj @@ -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)