mirror of
https://github.com/status-im/re-frame.git
synced 2025-02-23 23:38:11 +00:00
Get logging working
This commit is contained in:
parent
be8f5b1cc5
commit
36b88a7378
@ -1,34 +1,40 @@
|
|||||||
(ns re-frame.utils)
|
(ns re-frame.utils
|
||||||
|
(:require
|
||||||
|
[clojure.set :refer [difference]]))
|
||||||
|
|
||||||
|
|
||||||
;; -- Logging -----------------------------------------------------------------
|
;; -- Logging -----------------------------------------------------------------
|
||||||
;;
|
;;
|
||||||
;; re-frame uses various kinds of logging.
|
;; re-frame internally uses a set of logging functions which, by default,
|
||||||
;; Call set-loggers! if you want to override what it uses.
|
;; 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
|
(def default-loggers
|
||||||
{:log js/console.log
|
{:log #(.log js/console %)
|
||||||
:warn js/console.warn
|
:warn #(.warn js/console %)
|
||||||
:error js/console.error
|
:error #(.error js/console %)
|
||||||
:group (or js/console.group js/console.log) ;; group does not exist < IE 11
|
:group #(if (.group js/console) (.group js/console %) (.log js/console %)) ;; group does not exist < IE 11
|
||||||
:groupEnd (or js/console.groupEnd #())}) ;; groupEnd 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.
|
;; holds the current set of loggers.
|
||||||
(def loggers (atom default-loggers))
|
(def loggers (atom default-loggers))
|
||||||
|
|
||||||
(defn set-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]
|
[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))
|
(swap! loggers merge new-loggers))
|
||||||
|
|
||||||
|
|
||||||
(defn log [& args] ({:log loggers} (apply str args)))
|
(defn log [& args] ((:log @loggers) (apply str args)))
|
||||||
(defn warn [& args] ({:warn 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 group [& args] ({:group loggers} (apply str args)))
|
(defn groupEnd [& args] ((:groupEnd @loggers) (apply str args)))
|
||||||
(defn groupEnd [& args] ({:groupEnd loggers} (apply str args)))
|
(defn error [& args] ((:error @loggers) (apply str args)))
|
||||||
|
|
||||||
|
|
||||||
;; -- Misc --------------------------------------------------------------------
|
;; -- Misc --------------------------------------------------------------------
|
||||||
|
|
||||||
@ -36,5 +42,5 @@
|
|||||||
[v]
|
[v]
|
||||||
(if (vector? v)
|
(if (vector? v)
|
||||||
(first v)
|
(first v)
|
||||||
(warn "re-frame: expected a vector event, but got: " v)))
|
(error "re-frame: expected a vector event, but got: " v)))
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user