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..60c2ed0 100644 --- a/lib/lograge.rb +++ b/lib/lograge.rb @@ -116,7 +116,27 @@ 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) + + [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 28e3f0e..9ac045a 100644 --- a/spec/log_subscribers/action_cable_spec.rb +++ b/spec/log_subscribers/action_cable_spec.rb @@ -75,8 +75,16 @@ context 'when an `ActiveRecord::RecordNotFound` is raised' do let(:exception) { 'ActiveRecord::RecordNotFound' } + around do |example| + original_rescue_responses = ActionDispatch::ExceptionWrapper.rescue_responses + ActionDispatch::ExceptionWrapper.rescue_responses = + original_rescue_responses.merge(exception => :not_found) + example.run + ensure + ActionDispatch::ExceptionWrapper.rescue_responses = original_rescue_responses + end + before do - ActionDispatch::ExceptionWrapper.rescue_responses[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..35b54ec 100644 --- a/spec/log_subscribers/action_controller_spec.rb +++ b/spec/log_subscribers/action_controller_spec.rb @@ -146,8 +146,16 @@ context 'when an `ActiveRecord::RecordNotFound` is raised' do let(:exception) { 'ActiveRecord::RecordNotFound' } + around do |example| + original_rescue_responses = ActionDispatch::ExceptionWrapper.rescue_responses + ActionDispatch::ExceptionWrapper.rescue_responses = + original_rescue_responses.merge(exception => :not_found) + example.run + ensure + ActionDispatch::ExceptionWrapper.rescue_responses = original_rescue_responses + end + before do - ActionDispatch::ExceptionWrapper.rescue_responses[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