Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow logging Operation#wtf? #45

Open
phillipoertel opened this issue Jul 13, 2024 · 2 comments
Open

Allow logging Operation#wtf? #45

phillipoertel opened this issue Jul 13, 2024 · 2 comments

Comments

@phillipoertel
Copy link

phillipoertel commented Jul 13, 2024

Hey there,

our developers love #wtf? and leave it in even after feature development is complete, to reproduce and debug on staging/prod what was going on, in case something fails (it happens ;-D).

Problem is, the #wtf output messes up the output of tests, particularly rspec when run in --format documentation mode, and the #wtf output can't be turned off easily at the moment.

So my request is:

  • allow configuring where the output of #wtf? goes -- to STDOUT by default (backwards compatibility), to a logger (i.e. allow assigning a logger that the wtf output gets sent to), or somewhere else (silencing it by setting the config to nil/false)
  • I think the log level should be debug or at most info
  • (optional) if running in Rails context, use it's logger by default

One thing I'm not so sure about is how to preserve the colorized, "ASCII-art" output of wtf in a log message. As is, will be quite hard to read and piece together if sent to a logger. In the simplest case, just make sure all of output ends up in a single log message, including the ASCII colorization codes and newlines. Then it will be fairly easy to copy out and restore. Serializing such a tree structure to a (JSON) string may be another option; but that's possible overkill and will likely be harder to read anyways. Maybe you additional ideas. What wouldn't work well is to send each line of output as a separate message, it will be hard to piece together again.

I've attached a screenshot of a typical #wtf? output for reference:

Bildschirmfoto 2024-07-13 um 11 17 28

And here's the monkeypatch we currently use to silence #wtf?, as it may be helpful to others. It works in the context of a Rails application using the dotenv gem for configuration.

In RAILS_ROOT/.env:

[...]
# turn on to suppress trailblazer's wtf? output (in specs, for example)
SILENCE_TRAILBLAZER_WTF=true

In RAILS_ROOT/config/initializers/trailblazer.rb

# monkeypatch to silence the trailblazer wtf? output
module Trailblazer::Developer
  module Wtf
    module_function

    def puts(string)
      (ENV["SILENCE_TRAILBLAZER_WTF"] == "true") ? nil : super
    end
  end
end
@Drowze
Copy link

Drowze commented Nov 18, 2024

+1 to this!

While I love #wtf?, I'd prefer if it was easier to programatically print its output based on some custom logic (say - only print it when a test fails)

I'll probably resort to some monkey patch for now, but a more flexible API would definitely be preferable 😄

@apotonick
Copy link
Member

apotonick commented Nov 20, 2024

@Drowze Thanks for your input! 🍻 We actually want to solve this problem cleanly by changing the way operations are (or should be) invoked. With endpoints, the operation is basically not called directly anymore:

# just an example, where TRB is an imaginary endpoint constant
TRB._(Memo::Operation::Create, params: {here  we go})

In most cases, this could be hidden, either behind Controller#run or, in a test, behind an #assert_run abstraction or your own helpers. While this might feel clumsy, it's actually much much cleaner than monkeypatching all the things.

Any ideas or objections about this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants