rails_semantic_logger
rails_semantic_logger copied to clipboard
Unsubscribe all listeners in Rails >= 7.1
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 https://github.com/rails/rails/pull/45796 to the listeners_for method, which rails_semantic_logger uses to unsubscribe existing log subscribers. In short:
- With
log_level = :fatal, whenrails_semantic_loggeris first loaded,listeners_fordoesn'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.