From 945f1e8c751f7dd5a812066a64e339acae63aaff Mon Sep 17 00:00:00 2001 From: John Nagro Date: Sat, 14 Nov 2026 16:14:52 -0500 Subject: [PATCH 1/2] Remove Rails' event reporter log subscribers on Rails 8.2+ Rails 8.2 (rails/rails#55900) moved ActionController::LogSubscriber and ActionView::LogSubscriber onto ActiveSupport::EventReporter::LogSubscriber, subscribed to ActiveSupport.event_reporter rather than ActiveSupport::Notifications. remove_existing_log_subscriptions no longer found them, so Rails' "Processing by" / "Completed" lines were logged alongside lograge's line. Also unsubscribe these classes from ActiveSupport.event_reporter when they are event reporter log subscribers. The Notifications-based removal for earlier Rails versions is unchanged. Update the specs for both subscriber systems, and stop mutating ExceptionWrapper.rescue_responses in place, since Rails main freezes it. Fixes #407 --- CHANGELOG.md | 1 + lib/lograge.rb | 19 ++++ spec/log_subscribers/action_cable_spec.rb | 3 +- .../log_subscribers/action_controller_spec.rb | 3 +- spec/lograge_spec.rb | 103 +++++++++++++----- 5 files changed, 98 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 29f7e4d..cbca22d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Keep logging when the JSON and Cee formatters receive data that the json gem refuses to generate, such as non-ASCII binary strings (rejected since json 3.1) or invalid UTF-8. Affected strings, symbols and objects (such as exceptions with binary messages) are converted to UTF-8, replacing invalid bytes, instead of the whole log line being lost [#409](https://github.com/roidrage/lograge/issues/409) * String keys from `custom_options` and `custom_payload` now override built-in Symbol keys of the same name (and vice versa) instead of being logged twice, which json 3.0 rejects as duplicate keys. The value is stored under the key that was already present, so Lograge's formatters still see it [#410](https://github.com/roidrage/lograge/issues/410) +* Remove Rails' default Action Controller and Action View request logs on Rails 8.2+, where these log subscribers are subscribed to `ActiveSupport.event_reporter` instead of `ActiveSupport::Notifications` [#407](https://github.com/roidrage/lograge/issues/407) ### 0.15.1 diff --git a/lib/lograge.rb b/lib/lograge.rb index f86767d..6de5d6d 100644 --- a/lib/lograge.rb +++ b/lib/lograge.rb @@ -116,6 +116,25 @@ def remove_existing_log_subscriptions unsubscribe(:action_controller, subscriber) end end + + remove_event_reporter_log_subscriptions + end + + # Rails 8.2+ framework log subscribers inherit from + # ActiveSupport::EventReporter::LogSubscriber and are subscribed to + # ActiveSupport.event_reporter instead of ActiveSupport::Notifications. + def remove_event_reporter_log_subscriptions + rails_event_reporter_log_subscriber_classes.each do |klass| + ActiveSupport.event_reporter.unsubscribe(klass) + end + end + + def rails_event_reporter_log_subscriber_classes + return [] unless defined?(ActiveSupport::EventReporter::LogSubscriber) && ActiveSupport.respond_to?(:event_reporter) + + [ActionController::LogSubscriber, ActionView::LogSubscriber].select do |klass| + klass < ActiveSupport::EventReporter::LogSubscriber + end end def unsubscribe(component, subscriber) diff --git a/spec/log_subscribers/action_cable_spec.rb b/spec/log_subscribers/action_cable_spec.rb index 28e3f0e..e249f52 100644 --- a/spec/log_subscribers/action_cable_spec.rb +++ b/spec/log_subscribers/action_cable_spec.rb @@ -76,7 +76,8 @@ let(:exception) { 'ActiveRecord::RecordNotFound' } before do - ActionDispatch::ExceptionWrapper.rescue_responses[exception] = :not_found + ActionDispatch::ExceptionWrapper.rescue_responses = + ActionDispatch::ExceptionWrapper.rescue_responses.merge(exception => :not_found) event.payload[:exception] = [exception, 'Record not found'] event.payload[:status] = nil end diff --git a/spec/log_subscribers/action_controller_spec.rb b/spec/log_subscribers/action_controller_spec.rb index c41108b..f2ff58b 100644 --- a/spec/log_subscribers/action_controller_spec.rb +++ b/spec/log_subscribers/action_controller_spec.rb @@ -147,7 +147,8 @@ let(:exception) { 'ActiveRecord::RecordNotFound' } before do - ActionDispatch::ExceptionWrapper.rescue_responses[exception] = :not_found + ActionDispatch::ExceptionWrapper.rescue_responses = + ActionDispatch::ExceptionWrapper.rescue_responses.merge(exception => :not_found) event.payload[:exception] = [exception, 'Record not found'] event.payload[:status] = nil end diff --git a/spec/lograge_spec.rb b/spec/lograge_spec.rb index 94471e9..80474c6 100644 --- a/spec/lograge_spec.rb +++ b/spec/lograge_spec.rb @@ -12,47 +12,92 @@ require 'action_view/log_subscriber' describe Lograge do - context "when removing Rails' log subscribers" do - after do - ActionController::LogSubscriber.attach_to :action_controller - ActionView::LogSubscriber.attach_to :action_view - end + event_reporter_log_subscribers = + defined?(ActiveSupport::EventReporter::LogSubscriber) && + ActionController::LogSubscriber < ActiveSupport::EventReporter::LogSubscriber + + if event_reporter_log_subscribers + # Rails 8.2+ + context "when removing Rails' event reporter log subscribers" do + let(:rails_log_subscriber_classes) { [ActionController::LogSubscriber, ActionView::LogSubscriber] } + + def subscribers_of(klass) + ActiveSupport.event_reporter.subscribers.select { |entry| entry[:subscriber].is_a?(klass) } + end + + after do + rails_log_subscriber_classes.each do |klass| + next if subscribers_of(klass).any? + + ActiveSupport.event_reporter.subscribe(klass.new, &klass.subscription_filter) + end + end + + it 'removes the Action Controller and Action View log subscribers' do + expect(subscribers_of(ActionController::LogSubscriber)).not_to be_empty + expect(subscribers_of(ActionView::LogSubscriber)).not_to be_empty - it 'removes subscribers for controller events' do - expect do Lograge.remove_existing_log_subscriptions - end.to change { - Lograge.notification_listeners_for('process_action.action_controller') - }.to([]) - end - it 'removes subscribers for all events' do - expect do + expect(subscribers_of(ActionController::LogSubscriber)).to be_empty + expect(subscribers_of(ActionView::LogSubscriber)).to be_empty + end + + it 'does not remove other event reporter subscribers' do + custom_subscriber = Class.new { def emit(_event); end }.new + ActiveSupport.event_reporter.subscribe(custom_subscriber) + Lograge.remove_existing_log_subscriptions - end.to change { - Lograge.notification_listeners_for('render_template.action_view') - }.to([]) + + expect(ActiveSupport.event_reporter.subscribers.map { |entry| entry[:subscriber] }) + .to include(custom_subscriber) + ensure + ActiveSupport.event_reporter.unsubscribe(custom_subscriber) + end end + else + context "when removing Rails' log subscribers" do + after do + ActionController::LogSubscriber.attach_to :action_controller + ActionView::LogSubscriber.attach_to :action_view + end - shared_examples 'preserving non-Rails subscribers' do |event_name| - context "with event_name #{event_name}" do - 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) - anonymous_subscriber = - ActiveSupport::Notifications.subscribe(event_name, Class.new(CustomListener).new) + it 'removes subscribers for controller events' do + expect do + Lograge.remove_existing_log_subscriptions + end.to change { + Lograge.notification_listeners_for('process_action.action_controller') + }.to([]) + end + it 'removes subscribers for all events' do + expect do Lograge.remove_existing_log_subscriptions + end.to change { + Lograge.notification_listeners_for('render_template.action_view') + }.to([]) + end + + shared_examples 'preserving non-Rails subscribers' do |event_name| + context "with event_name #{event_name}" do + 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) + anonymous_subscriber = + ActiveSupport::Notifications.subscribe(event_name, Class.new(CustomListener).new) + + Lograge.remove_existing_log_subscriptions - listeners = Lograge.notification_listeners_for(event_name) - expect(listeners).to contain_exactly(proc_subscriber, custom_subscriber, anonymous_subscriber) + listeners = Lograge.notification_listeners_for(event_name) + expect(listeners).to contain_exactly(proc_subscriber, custom_subscriber, anonymous_subscriber) + end end end - end - include_examples 'preserving non-Rails subscribers', 'render_template.action_view' - include_examples 'preserving non-Rails subscribers', 'process_action.action_controller' + include_examples 'preserving non-Rails subscribers', 'render_template.action_view' + include_examples 'preserving non-Rails subscribers', 'process_action.action_controller' + end end describe 'keep_original_rails_log option' do From b9e193cdd55a9819ad28f2e3aa183661bee222c3 Mon Sep 17 00:00:00 2001 From: John Nagro Date: Sat, 14 Nov 2026 16:22:53 -0500 Subject: [PATCH 2/2] Keep event reporter helpers private and restore rescue_responses in specs Mark the two new event reporter helpers as private class methods so they don't become part of Lograge's public API, and drop the redundant ActiveSupport.respond_to?(:event_reporter) check. Restore ActionDispatch::ExceptionWrapper.rescue_responses after the 404 specs so the RecordNotFound mapping doesn't leak into other examples. --- lib/lograge.rb | 3 ++- spec/log_subscribers/action_cable_spec.rb | 11 +++++++++-- spec/log_subscribers/action_controller_spec.rb | 11 +++++++++-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/lograge.rb b/lib/lograge.rb index 6de5d6d..60c2ed0 100644 --- a/lib/lograge.rb +++ b/lib/lograge.rb @@ -130,12 +130,13 @@ def remove_event_reporter_log_subscriptions end def rails_event_reporter_log_subscriber_classes - return [] unless defined?(ActiveSupport::EventReporter::LogSubscriber) && ActiveSupport.respond_to?(:event_reporter) + return [] unless defined?(ActiveSupport::EventReporter::LogSubscriber) [ActionController::LogSubscriber, ActionView::LogSubscriber].select do |klass| klass < ActiveSupport::EventReporter::LogSubscriber end end + private_class_method :remove_event_reporter_log_subscriptions, :rails_event_reporter_log_subscriber_classes def unsubscribe(component, subscriber) events = subscriber.public_methods(false).reject { |method| method.to_s == 'call' } diff --git a/spec/log_subscribers/action_cable_spec.rb b/spec/log_subscribers/action_cable_spec.rb index e249f52..9ac045a 100644 --- a/spec/log_subscribers/action_cable_spec.rb +++ b/spec/log_subscribers/action_cable_spec.rb @@ -75,9 +75,16 @@ context 'when an `ActiveRecord::RecordNotFound` is raised' do let(:exception) { 'ActiveRecord::RecordNotFound' } - before do + around do |example| + original_rescue_responses = ActionDispatch::ExceptionWrapper.rescue_responses ActionDispatch::ExceptionWrapper.rescue_responses = - ActionDispatch::ExceptionWrapper.rescue_responses.merge(exception => :not_found) + original_rescue_responses.merge(exception => :not_found) + example.run + ensure + ActionDispatch::ExceptionWrapper.rescue_responses = original_rescue_responses + end + + before do event.payload[:exception] = [exception, 'Record not found'] event.payload[:status] = nil end diff --git a/spec/log_subscribers/action_controller_spec.rb b/spec/log_subscribers/action_controller_spec.rb index f2ff58b..35b54ec 100644 --- a/spec/log_subscribers/action_controller_spec.rb +++ b/spec/log_subscribers/action_controller_spec.rb @@ -146,9 +146,16 @@ context 'when an `ActiveRecord::RecordNotFound` is raised' do let(:exception) { 'ActiveRecord::RecordNotFound' } - before do + around do |example| + original_rescue_responses = ActionDispatch::ExceptionWrapper.rescue_responses ActionDispatch::ExceptionWrapper.rescue_responses = - ActionDispatch::ExceptionWrapper.rescue_responses.merge(exception => :not_found) + original_rescue_responses.merge(exception => :not_found) + example.run + ensure + ActionDispatch::ExceptionWrapper.rescue_responses = original_rescue_responses + end + + before do event.payload[:exception] = [exception, 'Record not found'] event.payload[:status] = nil end