From 8a57f8590fc88dce6867f5e7caa1dabcf08c845e Mon Sep 17 00:00:00 2001 From: anatoly Date: Mon, 7 Dec 2015 15:57:51 -0500 Subject: [PATCH] #10: formating audit logs (joda/hiccup and friends) --- .gitignore | 2 +- project.clj | 2 ++ test/mount/example/app.cljs | 20 +++++++++++++++----- test/mount/example/audit_log.cljs | 5 +++-- test/mount/example/websockets.cljs | 14 +++++++++----- 5 files changed, 30 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index f7c8d0f..45cf3f2 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,7 @@ pom.xml pom.xml.asc .repl* -/dev-resources/resources/public/js/* +dev-resources/public/js/* figwheel_server.log *.jar *.class diff --git a/project.clj b/project.clj index 100c6a9..36f25f2 100644 --- a/project.clj +++ b/project.clj @@ -11,6 +11,8 @@ :profiles {:dev {:source-paths ["dev" "test/app"] :dependencies [[datascript "0.13.3"] + [hiccups "0.3.0"] + [com.andrewmcveigh/cljs-time "0.3.14"] [ch.qos.logback/logback-classic "1.1.3"] [org.clojure/tools.logging "0.3.1"] [robert/hooke "1.3.0"] diff --git a/test/mount/example/app.cljs b/test/mount/example/app.cljs index e0027a8..20f18c2 100644 --- a/test/mount/example/app.cljs +++ b/test/mount/example/app.cljs @@ -2,15 +2,25 @@ (:require [mount.core :as mount] [mount.example.app-config] [mount.example.websockets] - [mount.example.audit-log :refer [log find-all-logs]])) + [mount.example.audit-log :refer [log find-all-logs]] + [cljs-time.format :refer [unparse formatters]] + [hiccups.runtime :as hiccupsrt]) + (:require-macros [hiccups.core :as hiccups :refer [html]])) + +(defn format-log-event [{:keys [timestamp source msg]}] + (str (unparse (formatters :date-hour-minute-second-fraction) timestamp) + " → [" (name source) "]: " msg)) (defn show-log [] (.write js/document - (interpose "
" (find-all-logs log)))) + (html [:ul (doall (for [e (find-all-logs log)] + [:li (format-log-event e)]))]))) (mount/start) -;; time for websocket to connect -(js/setTimeout #(do (mount/stop-except "#'mount.example.audit-log/log") - (show-log)) 500) +;; time to establish a websocket connection before disconnecting +(js/setTimeout #(mount/stop-except "#'mount.example.audit-log/log") 500) + +;; time to close a connection to show it in audit +(js/setTimeout #(show-log) 1000) diff --git a/test/mount/example/audit_log.cljs b/test/mount/example/audit_log.cljs index 1e29ee7..f4525ec 100644 --- a/test/mount/example/audit_log.cljs +++ b/test/mount/example/audit_log.cljs @@ -1,5 +1,6 @@ (ns mount.example.audit-log - (:require [datascript.core :as d]) + (:require [datascript.core :as d] + [cljs-time.core :refer [now]]) (:require-macros [mount.core :refer [defstate]])) (defstate log :start (d/create-conn {})) @@ -7,7 +8,7 @@ (defn audit [db source & msg] (d/transact! @db [{:db/id -1 :source source - :timestamp (js/Date.) + :timestamp (now) :msg (apply str msg)}])) (defn find-source-logs [db source] diff --git a/test/mount/example/websockets.cljs b/test/mount/example/websockets.cljs index df90485..49cd25a 100644 --- a/test/mount/example/websockets.cljs +++ b/test/mount/example/websockets.cljs @@ -3,16 +3,20 @@ [mount.example.audit-log :refer [audit log]]) (:require-macros [mount.core :refer [defstate]])) +(defn ws-status [ws] + {:url (.-url ws) :ready-state (.-readyState ws)}) + (defn connect [uri] - (audit log :system-a "connecting to '" uri "'") (let [ws (js/WebSocket. uri)] - (set! (.-onopen ws) #(audit log :system-a "opening ws @" uri)) - (set! (.-onclose ws) #(audit log :system-a "closing ws @" uri)) + (audit log :system-a "connecting to " (ws-status ws)) + (set! (.-onopen ws) #(audit log :system-a "opened " (ws-status ws))) + (set! (.-onclose ws) #(audit log :system-a "closed " (ws-status ws))) ws)) (defn disconnect [ws] - (audit log "disconnecting " @ws) - (.close @ws)) + (audit log :system-a "closing " (ws-status @ws)) + (.close @ws) + (audit log :system-a "disconnecting " (ws-status @ws))) (defstate system-a :start (connect (get-in @config [:system-a :uri])) :stop (disconnect system-a))