Get logging working

This commit is contained in:
mike-thompson-day8 2015-05-02 10:47:37 +10:00
parent be8f5b1cc5
commit 36b88a7378
1 changed files with 23 additions and 17 deletions

View File

@ -1,34 +1,40 @@
(ns re-frame.utils)
(ns re-frame.utils
(:require
[clojure.set :refer [difference]]))
;; -- Logging -----------------------------------------------------------------
;;
;; re-frame uses various kinds of logging.
;; Call set-loggers! if you want to override what it uses.
;; re-frame internally uses a set of logging functions which, by default,
;; print to js/console.
;; Use set-loggers! if you want to change this default behaviour.
;; In production environment, you may want to capture exceptions and POST
;; them somewhere. to , you might want to override the way that exceptions are
;; handled by overridding "error"
;;
(def default-loggers
{:log js/console.log
:warn js/console.warn
:error js/console.error
:group (or js/console.group js/console.log) ;; group does not exist < IE 11
:groupEnd (or js/console.groupEnd #())}) ;; groupEnd does not exist < IE 11
{:log #(.log js/console %)
:warn #(.warn js/console %)
:error #(.error js/console %)
:group #(if (.group js/console) (.group js/console %) (.log js/console %)) ;; group does not exist < IE 11
:groupEnd #(when (.groupEnd js/console) (.groupEnd js/console))}) ;; groupEnd does not exist < IE 11
;; holds the current set of loggers.
(def loggers (atom default-loggers))
(defn set-loggers!
"new-loggers should be a map which looks like default-loggers"
"Change the set (subset?) of logging functions used by re-frame.
'new-loggers' should be a map which looks like default-loggers"
[new-loggers]
(assert (empty? (clojure.set/difference (set (keys new-loggers)) (set (keys default-loggers)))) "Unknown keys in new-loggers")
(assert (empty? (difference (set (keys new-loggers)) (set (keys default-loggers)))) "Unknown keys in new-loggers")
(swap! loggers merge new-loggers))
(defn log [& args] ({:log loggers} (apply str args)))
(defn warn [& args] ({:warn loggers} (apply str args)))
(defn error [& args] ({:error loggers} (apply str args)))
(defn group [& args] ({:group loggers} (apply str args)))
(defn groupEnd [& args] ({:groupEnd loggers} (apply str args)))
(defn log [& args] ((:log @loggers) (apply str args)))
(defn warn [& args] ((:warn @loggers) (apply str args)))
(defn group [& args] ((:group @loggers) (apply str args)))
(defn groupEnd [& args] ((:groupEnd @loggers) (apply str args)))
(defn error [& args] ((:error @loggers) (apply str args)))
;; -- Misc --------------------------------------------------------------------
@ -36,5 +42,5 @@
[v]
(if (vector? v)
(first v)
(warn "re-frame: expected a vector event, but got: " v)))
(error "re-frame: expected a vector event, but got: " v)))