timbre icon indicating copy to clipboard operation
timbre copied to clipboard

[Cljs] Add support for console.group?

Open seantempesta opened this issue 7 years ago • 3 comments

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)))

seantempesta avatar Nov 21 '16 03:11 seantempesta

Hi Sean,

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

(trace "database state" {:conn conn :port port :host host})

ptaoussanis avatar Dec 17 '16 12:12 ptaoussanis

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 avatar Dec 17 '16 12:12 ptaoussanis

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

ptaoussanis avatar Dec 17 '16 12:12 ptaoussanis