Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions spec/logtail-rails/action_view/log_subscriber_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions spec/logtail-rails/http_events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/logtail-rails/rack_logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading