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: JS objects are printed uselessly #356

Closed DerGuteMoritz closed 1 year ago

DerGuteMoritz commented 1 year ago

Plain JS objects are printed rather uselessly by default in cljs. E.g.

(log/info #js {:foo "bar"})

which results in:

2022-09-27T14:04:14.746Z INFO [user:1] - [object Object]

JS arrays are also a bit odd in that they lack delimiters. E.g.

(log/info #js [1 2 3])

which results in:

2022-09-27T14:04:15.919Z INFO [user:1] - 1,2,3

We've worked around this with the following middleware so far:

(defn pr-js-objects-in-log-args
  "This is needed because timbre internally calls `toString` on log
  args, which means plain JS objects are logged as `[object Object]`."
  [data]
  (update data :vargs (fn [vargs]
                        (map (fn [arg]
                               (if (string? arg)
                                 arg
                                 (pr-str arg)))
                             vargs))))

Which makes the above examples print this instead:

2022-09-27T14:05:43.211Z INFO [user:1] - #js {:foo "bar"}
2022-09-27T14:05:44.023Z INFO [user:1] - #js [1 2 3]

Maybe worth including something like this by default?

ptaoussanis commented 1 year ago

@DerGuteMoritz Hi Moritz, thanks for pinging about this. I'd like a little time to think about the best way to address this.

My quick first impression:

Middleware will work, but may be needlessly expensive. In principle, might be better to transform vargs only when the :msg_ delay is actually evaluated.

Could provide some kind of option for a transform at that point, though need to think about the possibilities and tradeoffs.

Thoughts/ideas also welcome!

ptaoussanis commented 1 year ago

This will be addressed in forthcoming release.

Two relevant changes:

  1. The default behaviour will be adjusted to match yours here.
  2. It'll be possible to swap in an alternative message fn.

Thanks again for pinging about this 👍