taoensso / timbre

Pure Clojure/Script logging library
https://www.taoensso.com/timbre
Eclipse Public License 1.0
1.44k stars 171 forks source link

[Cljs] Add support for console.group? #204

Open seantempesta opened 7 years ago

seantempesta commented 7 years ago

During development it's super useful to do something like this:

(trace "database state" conn port host)

and get back:

console.group("database state")
console.log("conn", conn)
console.log("port", port)
console.log("host", host)
console.groupEnd();

as it's easily readable on the Chrome console what variable is what. Is there a way to get the symbol names (strings are fine) so I can print them? I created a macro to do this, but this feels hacky.

 (defmacro trace [& args]
     (let [first-arg (first args)
           string-arg (if (string? first-arg) first-arg "Trace")
           rest-args (if (string? first-arg) (rest args) args)
           group-start `(oops.core/ocall js/console "group" ~string-arg)
           groups (cons 'do (map (fn [arg] `(oops.core/ocall js/console "log" (quote ~arg) ~arg)) rest-args))
           group-end `(oops.core/ocall js/console "groupEnd")
           group-call (cons 'do [group-start groups group-end])
           normal-call `(taoensso.timbre/trace ~args)
           current-ns  (str (ns-name *ns*))]
       `(if (and js/goog.DEBUG (= :trace (:level taoensso.timbre/*config*)) (some #(= ~current-ns %) (:ns-whitelist  taoensso.timbre/*config*)))
          ~group-call
          ~normal-call)))
ptaoussanis commented 7 years ago

Hi Sean,

Is there a reason you wouldn't want something like this?:

(trace "database state" {:conn conn :port port :host host})
ptaoussanis commented 7 years ago

Oh, I see- you're specifically asking about console.group. Yeah, sorry- that's not something Timbre currently has support for.

Not familiar with console.group myself: it's capabilities, support, etc. Would be open to a an API suggestion, etc. with motivation.

Thanks! :-)

ptaoussanis commented 7 years ago

BTW please ping/upvote if there's general demand for this feature. Again, motivations/examples would be especially useful- cheers! :-)