Fork me on GitHub
#timbre
<
2024-03-14
>
pez09:03:10

The browser console appender prepends [namespace:line] to the logged output which is neat. However, it logs where the call to the logger is made, which in my case is in a logging namespace of my app. Is there something I can do to “pass along” the originating call site?

1
souenzzo22:03:16

If your namespace logging look like this:

(defmacro my-info [....]
    `(timbre/info ....))
you can do
(defmacro my-info [....]
  (-> `(timbre/info ....)
    (vary-meta merge (meta &form))))
Timbre by default logs the file/line that comes from &form metadata And you can "forward" this info from one macro to another I'm bad on explaining it. But it works.

pez22:03:43

Thanks! I’ll try it!

souenzzo22:03:38

^ more context

🙏 1
Peter Taoussanis13:03:49

Hi @U0ETXRFEW! And thanks @U2J4FRT2T 🙏 If the problem is that you’ve got a macro wrapping Timbre’s own macro/s, then you can use something like @U2J4FRT2T mentioned above - or use keep-callsite https://taoensso.github.io/encore/taoensso.encore.html#var-keep-callsite (you’ll already have Encore if you’re using Timbre). If the problem is that you’ve got a function wrapping Timbre’s own macro/s, then that’ll be more difficult since your callsite info won’t be retained through the function call. There might still be some viable options, but I’d need to know more about your specific setup (e.g. the wrapper code calling Timbre).

pez14:03:19

Hi! I had both these cases. But for case one I realized I could use Timbre’s macros directly without losing much of my convenience. I’m considering using @U2J4FRT2T’s trick to give me back the convenience I sacrificed. For case two it is a bit special maybe. I have a data driven event system, where I have defined a log event that I can dispatch like [:log/level :debug a b c], which will eventually call (timbre/log!:p :debug [a b c]) . So, unless there is something I can pass along in that event vector, I think I am out of luck?

souenzzo14:03:05

@U0ETXRFEW in clojure JVM you can easily inspect the stacktrace and get the original location where a function calls the my-log function with that info, you can add some extra keys to (timbre/log .... {:orig-line ...}) And with a middleware, replace the timbre's default lineno with your orig-line BUT not sure how it will work on JS I know that you can get the stacktrace from an exception in cljs. But it may contains linenumbers from the bundle, even in this case, you can get the name(space) of the function, that i think that is useful.

Peter Taoussanis14:03:10

> So, unless there is something I can pass along in that event vector, I think I am out of luck? So long as you actually have (or can get) the location info, you can provide it to Timbre via the :loc option of Timbre’s log! macro. There’s a https://taoensso.github.io/encore/taoensso.encore.html#var-get-source in Encore to capture callsite info in the appropriate format. One option would be to have a macro to generate events. That macro can then grab the callsite info and pass it along as part of the event.

pez14:03:39

Thanks both of you! 🙏 I’ll experiment a bit and see where it gets me.

👍 1