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

Unsubscribe all listeners in Rails >= 7.1 #252

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lucthev
Copy link

@lucthev lucthev commented Nov 25, 2024

Background

In the app I'm working on, we've (mostly) silenced logs in CI via

# config/environments/test.rb
if ENV['CI']
  config.log_level = :fatal
end

There are some tests that explicitly test logging, though, so for the duration of those test we drop the log level down back down to :info.

While upgrading this app from Rails 7 to Rails 7.1, we saw some odd failures in these tests — both the Rails-default logs and the rails_semantic_logger logs were being printed. It seems to be due to the changes made in rails/rails#45796 to the listeners_for method, which rails_semantic_logger uses to unsubscribe existing log subscribers. In short:

  • With log_level = :fatal, when rails_semantic_logger is first loaded, listeners_for doesn't return the Rails-default log subscribers, since they're currently silenced
  • When we drop the log level back down, those listeners are now un-silenced, and output their logs

Description of changes

This PR uses all_listeners_for, where available, to ensure that currently-silenced subscribers also get unsubscribed.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

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

Successfully merging this pull request may close these issues.

1 participant