From 3a770b219748835863cfc116af68bff46835b2a1 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Mon, 29 Mar 2021 17:23:45 -0700 Subject: [PATCH] Fix LogSubscriber for buffered event w/ nil logger LogSubscriber overrides start/finish to avoid instrumenting when its logger is nil. In order to support buffered notification events, as used by async queries, we need to apply a similar override to LogSubscriber#publish_event. --- .../lib/active_support/log_subscriber.rb | 16 +++++++++++++--- activesupport/test/log_subscriber_test.rb | 11 +++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/activesupport/lib/active_support/log_subscriber.rb b/activesupport/lib/active_support/log_subscriber.rb index af90bccebf..29a6941c04 100644 --- a/activesupport/lib/active_support/log_subscriber.rb +++ b/activesupport/lib/active_support/log_subscriber.rb @@ -114,9 +114,13 @@ module ActiveSupport def finish(name, id, payload) super if logger rescue => e - if logger - logger.error "Could not log #{name.inspect} event. #{e.class}: #{e.message} #{e.backtrace}" - end + log_exception(name, e) + end + + def publish_event(event) + super if logger + rescue => e + log_exception(event.name, e) end private @@ -138,5 +142,11 @@ module ActiveSupport bold = bold ? BOLD : "" "#{bold}#{color}#{text}#{CLEAR}" end + + def log_exception(name, e) + if logger + logger.error "Could not log #{name.inspect} event. #{e.class}: #{e.message} #{e.backtrace}" + end + end end end diff --git a/activesupport/test/log_subscriber_test.rb b/activesupport/test/log_subscriber_test.rb index d4bd082def..0f51c6aaa7 100644 --- a/activesupport/test/log_subscriber_test.rb +++ b/activesupport/test/log_subscriber_test.rb @@ -107,6 +107,17 @@ class SyncLogSubscriberTest < ActiveSupport::TestCase end end + def test_does_not_send_buffered_events_if_logger_is_nil + ActiveSupport::LogSubscriber.logger = nil + assert_not_called(@log_subscriber, :some_event) do + ActiveSupport::LogSubscriber.attach_to :my_log_subscriber, @log_subscriber + buffer = ActiveSupport::Notifications.instrumenter.buffer + buffer.instrument "some_event.my_log_subscriber" + buffer.flush + wait + end + end + def test_does_not_fail_with_non_namespaced_events ActiveSupport::LogSubscriber.attach_to :my_log_subscriber, @log_subscriber instrument "whatever"