Skip to content

Commit

Permalink
[#15]: adding logging AOP for REPL examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tolitius committed Nov 23, 2015
1 parent 41d4c13 commit 2d1f77d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions dev/dev.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
Expand Down
1 change: 1 addition & 0 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 51 additions & 0 deletions test/app/utils/logging.clj
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 2d1f77d

Please sign in to comment.