diff --git a/lib/logtail-rails/action_view/log_subscriber/logtail_log_subscriber.rb b/lib/logtail-rails/action_view/log_subscriber/logtail_log_subscriber.rb index 7fb451f..91fedd2 100755 --- a/lib/logtail-rails/action_view/log_subscriber/logtail_log_subscriber.rb +++ b/lib/logtail-rails/action_view/log_subscriber/logtail_log_subscriber.rb @@ -76,9 +76,12 @@ def self.attach_to(*) super if ::Rails::VERSION::MAJOR > 7 || ::Rails::VERSION::MAJOR == 7 && ::Rails::VERSION::MINOR >= 1 - # Clean extra listeners subscribed in parent's attach_to method - ::ActiveSupport::Notifications.notifier.listeners_for("render_template.action_view") - .concat(::ActiveSupport::Notifications.notifier.listeners_for("render_layout.action_view")).flatten + # Clean extra listeners subscribed in parent's attach_to method. Since Rails 7.2 they count as + # silenced while ActionView::Base.logger is nil, as it is at boot, and listeners_for skips those. + # all_listeners_for returns the notifier's cached array, so don't concat onto it. + notifier = ::ActiveSupport::Notifications.notifier + %w(render_template.action_view render_layout.action_view) + .flat_map { |name| notifier.respond_to?(:all_listeners_for) ? notifier.all_listeners_for(name) : notifier.listeners_for(name) } .filter { |listener| listener.delegate.class == ::ActionView::LogSubscriber::Start } .each { |listener| ActiveSupport::Notifications.unsubscribe(listener) } end diff --git a/spec/logtail-rails/action_view/log_subscriber_spec.rb b/spec/logtail-rails/action_view/log_subscriber_spec.rb index 29d0b5e..08b06f0 100755 --- a/spec/logtail-rails/action_view/log_subscriber_spec.rb +++ b/spec/logtail-rails/action_view/log_subscriber_spec.rb @@ -75,6 +75,36 @@ def method_for_action(action_name) end end end + + context "with a debug level" do + around(:each) do |example| + old_level = logger.level + logger.level = ::Logger::DEBUG + example.run + logger.level = old_level + end + + it "should not log the start of the rendering" do + dispatch_rails_request("/action_view_log_subscriber") + expect(io.string.scan("Rendered spec/support/rails/templates/template.html").length).to eq(1) + expect(io.string).not_to include("Rendering") + end + end + end + end + + if defined?(::ActionView::LogSubscriber::Start) + describe "integrate!" do + # Since Rails 7.1, ActionView::LogSubscriber.attach_to subscribes these listeners next to the + # subscriber itself. They log "Rendering ..." at debug level. + it "should remove the listeners logging the start of a rendering" do + notifier = ::ActiveSupport::Notifications.notifier + listeners = %w(render_template.action_view render_layout.action_view).flat_map do |name| + notifier.respond_to?(:all_listeners_for) ? notifier.all_listeners_for(name) : notifier.listeners_for(name) + end + + expect(listeners.map(&:delegate).grep(::ActionView::LogSubscriber::Start)).to be_empty + end end end diff --git a/spec/logtail-rails/http_events_spec.rb b/spec/logtail-rails/http_events_spec.rb index 2e8bb2a..d185ab9 100755 --- a/spec/logtail-rails/http_events_spec.rb +++ b/spec/logtail-rails/http_events_spec.rb @@ -45,7 +45,7 @@ def method_for_action(action_name) expect(lines[0]).to include("Started GET \\\"/rack_http\\\"") expect(lines[1]).to include("Processing by RackHttpController#index as HTML") - expect(lines[2]).to include("Completed 200 OK in 0.0ms") + expect(lines[2]).to match(/Completed 200 OK in \d+\.\d+ms/) end context "with the route silenced" do @@ -87,7 +87,7 @@ def method_for_action(action_name) expect(lines.length).to eq(2) expect(lines[0]).to include("Processing by RackHttpController#index as HTML") - expect(lines[1]).to include("GET /rack_http completed with 200 OK in 0.0ms") + expect(lines[1]).to match(/GET \/rack_http completed with 200 OK in \d+\.\d+ms/) end end end diff --git a/spec/logtail-rails/rack_logger_spec.rb b/spec/logtail-rails/rack_logger_spec.rb index e3c2109..6976159 100755 --- a/spec/logtail-rails/rack_logger_spec.rb +++ b/spec/logtail-rails/rack_logger_spec.rb @@ -49,7 +49,7 @@ def method_for_action(action_name) expect(lines.length).to eq(3) expect(lines[0]).to include("Started GET \\\"/rails_rack_logger\\\"") expect(lines[1]).to include("Processing by RailsRackLoggerController#index as HTML") - expect(lines[2]).to include("Completed 200 OK in 0.0ms") + expect(lines[2]).to match(/Completed 200 OK in \d+\.\d+ms/) ensure Logtail::Integrations::Rails::EventLogSubscriber.enabled = original_enabled end