Skip to content

Unsubscribe all Rails built-in subscribers - #386

Merged
johnnagro merged 3 commits into
roidrage:masterfrom
owst:unsubscribe_all_action_view_subscribers_in_rails_71
Sep 29, 2026
Merged

johnnagro merged 3 commits into
roidrage:masterfrom
owst:unsubscribe_all_action_view_subscribers_in_rails_71

Conversation

@owst

@owst owst commented Jan 24, 2024

Copy link
Copy Markdown
Contributor

Fixes #385

Assumes that a built-in subscriber is nested under the component namespace, so under ActionView or ActionController.

Fixes roidrage#385

Assumes that a built-in subscriber is nested under the component
namespace, so under ActionView or ActionController.
Comment thread spec/lograge_spec.rb
end.to change {
Lograge.notification_listeners_for('render_template.action_view')
}
}.to([])

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This spec failed after this change on Rails 7.1

Comment thread spec/lograge_spec.rb
it "does not remove subscribers that aren't from Rails" do
proc_subscriber = ActiveSupport::Notifications.subscribe(event_name, proc {})
custom_subscriber =
ActiveSupport::Notifications.subscribe(event_name, CustomListener.new)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also check a custom class, in case there was some special handling of Proc instances

Comment thread lib/lograge.rb Outdated
delegate = listener.instance_variable_get('@delegate')

# Assume that anything nested under the component namespace is built in to Rails
delegate.class.name.start_with?("#{component.to_s.classify}::")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is great, but I can't see a better way to check if the delegate is subscriber or (for Rails 7.1+) an instance of ActionView::LogSubscriber::Start for ActionView or subscriber for ActionController

@owst owst changed the title Unsubscribe all built-in subscribers Unsubscribe all Rails built-in subscribers Jan 24, 2024
@trobrock

Copy link
Copy Markdown

Hey any progress on this? I'm using the branch in production to help our logs. Would love to see this merged.

Anything I can help with?

@johnnagro

Copy link
Copy Markdown
Collaborator

@trobrock I've recently tapped in to help with this project. thanks for the PR, i will be taking a look shortly. i know its been kicking around for quite some time so let me know if anything about the situation has changed.

@hlascelles

Copy link
Copy Markdown

I think no change, this is still required 👍

johnnagro and others added 2 commits September 29, 2026 10:56
Replace the namespace-prefix check with an explicit match on the
subscriber instance or ActionView::LogSubscriber::Start (Rails 7.1+).
The prefix check raised NoMethodError for listeners whose class is
anonymous (Class#name is nil), and would also have removed any
non-log-subscriber listener nested under ActionView:: or
ActionController::.

Also add a spec covering anonymous-class listeners and the missing
frozen string literal comment in CustomListener.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@johnnagro

johnnagro commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks for this @owst, and sorry it sat for so long. I've pushed a couple of commits on top so we can get it merged:

  • Merged master into the branch (no conflicts, no force-push).
  • Replaced the namespace-prefix check with an explicit match: the listener's delegate is either the subscriber itself or an ActionView::LogSubscriber::Start (Rails 7.1+). The prefix check raised NoMethodError for listeners whose class is anonymous (Class#name is nil), which would break app boot. It would also have removed any non-log-subscriber listener nested under ActionView:: or ActionController::.
  • Added a spec covering an anonymous-class listener, and the missing # frozen_string_literal: true in spec/support/custom_listener.rb.

Tested locally on Rails 7.0, 7.1 and 8.1 (all green), and RuboCop is clean. Your original commit is preserved as-is.

@johnnagro
johnnagro merged commit a66e9bf into roidrage:master Sep 29, 2026
88 of 94 checks passed
@johnnagro johnnagro mentioned this pull request Sep 29, 2026
@johnnagro

Copy link
Copy Markdown
Collaborator

these changes landed in the 0.15.1 release which was just published. thanks for the contributions.

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.

ActionView logs not completely suppressed with Rails 7.1+

4 participants