vvvvalvalval / scope-capture

Project your Clojure(Script) REPL into the same context as your code when it ran
MIT License
576 stars 14 forks source link

Document how to register loggers #20

Closed DjebbZ closed 6 years ago

DjebbZ commented 6 years ago

Hi, long time no see IRL :)

I'm trying to slience the post-eval-logger because the data I want to inspect and play with is big, and logging it hides the Execution point haha.

I've tried registering globally a no-op function with (sc.api.logging/register-cs-logger :sc/spy-ep-post-eval-logger (fn [_])) but it didn't work. The only thing that worked was passing this function explicitly at call site of spy : (sc.api/spy {:sc/spy-ep-post-eval-logger (fn [_])} (my-code ...)).

Bug or mistake ? If it's a mistake, please correct me. In exchange you'll get a PR with documentation for this feature. Deal ?

DjebbZ commented 6 years ago

The bug exists in both "0.1.3" and "0.1.4".

And I think I have a hint at why. I'm using a REPL with the usual REPL-friendly reloaded workflow, but I've just remember that multimethods aren't REPL-friendly like that. So I've dived a little bit in the code and found that in spy-emit instead of calling the log-cs multimethod it directly calls sc.api.logging/log-spy-ep-post-eval or the version installed in the map. So registering a logger has no use here ?

vvvvalvalval commented 6 years ago

Hi, long time no see IRL :)

Hi! Yeah, a Paris Meetup should be announced soon :)

I've tried registering globally a no-op function with (sc.api.logging/register-cs-logger :sc/spy-ep-post-eval-logger (fn [_])) but it didn't work.

Yes, it's a mistake of usage, I think there are several misunderstandings to address here:

  1. What you want is to register a 'Post-Eval' logger, which implements the notification sent after evaluation at an Execution Point, at evaluation-time. However, you are using sc.api.logging/register-cs-logger, which is intended to define a 'Code Site' logger, which implements the notification sent when compiling a Code Site, at macro-expansion-time.
  2. In order to be a robust foundation for tooling, scope-capture does not currently allow you to "override global configuration" for loggers: instead, the recommended way to use your custom loggers is either to refer to them inline (as you did with (sc.api/spy {:sc/spy-ep-post-eval-logger (fn [_])} (my-code ...))) or to define your own spy macro with different defaults.

The Customization section of the Tutorial shows an example of defining both a custom Code Site Logger and a custom Post Eval Logger.

In your case, defining a silent Post-Eval Logger would consist of something like:

(defn noop-spy-post-eval 
  [_ep-data])

(def my-spy-opts
  ;; mind the syntax-quote '`'
  `{:sc/spy-ep-post-eval-logger noop-spy-post-eval})

(defmacro my-spy
  ([] (sc.api/spy-emit my-spy-opts nil &env &form))
  ([expr] (sc.api/spy-emit my-spy-opts expr &env &form))
  ([opts expr] (sc.api/spy-emit (merge my-spy-opts opts) expr &env &form)))

If you really wanted to change the configuration globally, you could use the following dark magic hack:

;; HACK changing the impl of the default Post Eval Logger via Var mutation
(alter-var-root #'sc.api.logging/log-spy-ep-post-eval 
  (constantly noop-spy-post-eval))

I would happily accept PRs that makes the doc clearer in this matter.

DjebbZ commented 6 years ago

My bad, I didn't see the Tutorial at all. What I ended up doing is copy-paste the slient Post-Eval logger snippet in my profile.boot and call (boot.user/my-spy (my-code ...)). The only difference is that I had to remove the syntax quote, otherwise it was thinking that the fully qualified symbol was from clojure.core. Dark macro magic...

I'll think on a PR.

vvvvalvalval commented 6 years ago

Alright, given that you didn't see the Tutorial I'll close this issue, but of course you are still welcome to open more specific issues or PRs regarding this topic.