Breaking change to logging

This commit is contained in:
Mike Thompson 2016-05-31 07:50:49 +10:00
parent da9c11eae0
commit 053771ad9d
2 changed files with 42 additions and 20 deletions

View File

@ -23,8 +23,27 @@ Headline:
Breaking:
- requires Reagent >= 0.6.0 or later. It won't work with <= Reagent 0.5.2.
- XXX undo extracted and put into sister library
- requires Reagent >= 0.6.0 or later. It won't work with <= Reagent 0.5.2.
- By default, re-frame uses `js/console` functions like `error` and `warn` when logging, but you can
supply alternatives via `re-frame.core/set-loggers!`.
With this release, alternatives you supply will be called with different parameter values.
Previously loggers were given a single `str` parameter but now they are expected to act
like `console.log` itself and take variadic, non string params. Sorry to break things, but
we are trying to maximise use of cljs-devtools and information is lost when strings are
output, instead of real data.
To transition, here's the tweak you can make:
```
;; your old version might have looked like this. Single string parameter.
(defn my-logger [s] (do-something-with s))
;; your new version will have variadic params, and turn them into a string
(defn myloggerr [& args] (do-something-with (apply str args))
```
Improvements
- XXXX middleware for spec checking of event vectors

View File

@ -1,25 +1,28 @@
(ns re-frame.utils
(:require
[clojure.set :refer [difference]]))
[clojure.set :refer [difference]]))
;; -- Logging -----------------------------------------------------------------
;;
;; re-frame internally uses a set of logging functions which, by default,
;; print to js/console.
;; re-frame uses a set of logging functions which are, by default, those from
;; 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 #(.log js/console %)
:warn #(.warn js/console %)
:error #(.error js/console %)
:group #(if (.-groupCollapsed js/console) (.groupCollapsed 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
(def default-log (js/console.log.bind js/console))
(def default-warn (js/console.warn.bind js/console))
(def default-error (js/console.error.bind js/console))
(def default-group (if (.-group js/console) (js/console.group.bind js/console) default-log)) ;; group does not exist < IE 11
(def default-groupEnd (if (.-groupEnd js/console) (js/console.groupEnd.bind js/console) #())) ;; groupEnd does not exist < IE 11
;; holds the current set of loggers.
(def default-loggers
{:log #(apply default-log %)
:warn #(apply default-warn %)
:error #(apply default-error %)
:group #(apply default-group %)
:groupEnd #(apply default-groupEnd %)})
;; holds the current set of loggin functions.
(def loggers (atom default-loggers))
(defn set-loggers!
@ -30,11 +33,11 @@
(swap! loggers merge new-loggers))
(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)))
(defn log [& args] ((:log @loggers) args))
(defn warn [& args] ((:warn @loggers) args))
(defn group [& args] ((:group @loggers) args))
(defn groupEnd [& args] ((:groupEnd @loggers) args))
(defn error [& args] ((:error @loggers) args))
;; -- Misc --------------------------------------------------------------------