1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #41793 from jhawthorn/fix_buffered_log_subscriber_nil

Fix LogSubscriber for buffered event w/ nil logger
This commit is contained in:
John Hawthorn 2021-03-30 09:48:29 -07:00 committed by GitHub
commit d89bb4f8ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 3 deletions

View file

@ -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

View file

@ -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"