#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
|
||||||
pom.xml.asc
|
pom.xml.asc
|
||||||
.repl*
|
.repl*
|
||||||
/dev-resources/resources/public/js/*
|
dev-resources/public/js/*
|
||||||
figwheel_server.log
|
figwheel_server.log
|
||||||
*.jar
|
*.jar
|
||||||
*.class
|
*.class
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,8 @@
|
||||||
|
|
||||||
:profiles {:dev {:source-paths ["dev" "test/app"]
|
:profiles {:dev {:source-paths ["dev" "test/app"]
|
||||||
:dependencies [[datascript "0.13.3"]
|
:dependencies [[datascript "0.13.3"]
|
||||||
|
[hiccups "0.3.0"]
|
||||||
|
[com.andrewmcveigh/cljs-time "0.3.14"]
|
||||||
[ch.qos.logback/logback-classic "1.1.3"]
|
[ch.qos.logback/logback-classic "1.1.3"]
|
||||||
[org.clojure/tools.logging "0.3.1"]
|
[org.clojure/tools.logging "0.3.1"]
|
||||||
[robert/hooke "1.3.0"]
|
[robert/hooke "1.3.0"]
|
||||||
|
|
|
||||||
|
|
@ -2,15 +2,25 @@
|
||||||
(:require [mount.core :as mount]
|
(:require [mount.core :as mount]
|
||||||
[mount.example.app-config]
|
[mount.example.app-config]
|
||||||
[mount.example.websockets]
|
[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 []
|
(defn show-log []
|
||||||
(.write js/document
|
(.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)
|
(mount/start)
|
||||||
|
|
||||||
;; time for websocket to connect
|
;; time to establish a websocket connection before disconnecting
|
||||||
(js/setTimeout #(do (mount/stop-except "#'mount.example.audit-log/log")
|
(js/setTimeout #(mount/stop-except "#'mount.example.audit-log/log") 500)
|
||||||
(show-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
|
(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]]))
|
(:require-macros [mount.core :refer [defstate]]))
|
||||||
|
|
||||||
(defstate log :start (d/create-conn {}))
|
(defstate log :start (d/create-conn {}))
|
||||||
|
|
@ -7,7 +8,7 @@
|
||||||
(defn audit [db source & msg]
|
(defn audit [db source & msg]
|
||||||
(d/transact! @db [{:db/id -1
|
(d/transact! @db [{:db/id -1
|
||||||
:source source
|
:source source
|
||||||
:timestamp (js/Date.)
|
:timestamp (now)
|
||||||
:msg (apply str msg)}]))
|
:msg (apply str msg)}]))
|
||||||
|
|
||||||
(defn find-source-logs [db source]
|
(defn find-source-logs [db source]
|
||||||
|
|
|
||||||
|
|
@ -3,16 +3,20 @@
|
||||||
[mount.example.audit-log :refer [audit log]])
|
[mount.example.audit-log :refer [audit log]])
|
||||||
(:require-macros [mount.core :refer [defstate]]))
|
(:require-macros [mount.core :refer [defstate]]))
|
||||||
|
|
||||||
|
(defn ws-status [ws]
|
||||||
|
{:url (.-url ws) :ready-state (.-readyState ws)})
|
||||||
|
|
||||||
(defn connect [uri]
|
(defn connect [uri]
|
||||||
(audit log :system-a "connecting to '" uri "'")
|
|
||||||
(let [ws (js/WebSocket. uri)]
|
(let [ws (js/WebSocket. uri)]
|
||||||
(set! (.-onopen ws) #(audit log :system-a "opening ws @" uri))
|
(audit log :system-a "connecting to " (ws-status ws))
|
||||||
(set! (.-onclose ws) #(audit log :system-a "closing ws @" uri))
|
(set! (.-onopen ws) #(audit log :system-a "opened " (ws-status ws)))
|
||||||
|
(set! (.-onclose ws) #(audit log :system-a "closed " (ws-status ws)))
|
||||||
ws))
|
ws))
|
||||||
|
|
||||||
(defn disconnect [ws]
|
(defn disconnect [ws]
|
||||||
(audit log "disconnecting " @ws)
|
(audit log :system-a "closing " (ws-status @ws))
|
||||||
(.close @ws))
|
(.close @ws)
|
||||||
|
(audit log :system-a "disconnecting " (ws-status @ws)))
|
||||||
|
|
||||||
(defstate system-a :start (connect (get-in @config [:system-a :uri]))
|
(defstate system-a :start (connect (get-in @config [:system-a :uri]))
|
||||||
:stop (disconnect system-a))
|
:stop (disconnect system-a))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue