#10: formating audit logs (joda/hiccup and friends)
This commit is contained in:
parent
d1d14bc710
commit
8a57f8590f
5 changed files with 30 additions and 13 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -4,7 +4,7 @@
|
|||
pom.xml
|
||||
pom.xml.asc
|
||||
.repl*
|
||||
/dev-resources/resources/public/js/*
|
||||
dev-resources/public/js/*
|
||||
figwheel_server.log
|
||||
*.jar
|
||||
*.class
|
||||
|
|
|
|||
|
|
@ -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"]
|
||||
|
|
|
|||
|
|
@ -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 "<br/>" (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)
|
||||
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
|
|||
Loading…
Reference in a new issue