From caed93edef66a7d7581e0483f935c821ff49707f Mon Sep 17 00:00:00 2001 From: Petr Heinz Date: Fri, 27 Oct 2023 11:42:35 +0200 Subject: [PATCH] L-873 Fix compatibility with Rails < 7.1 (#35) --- lib/logtail-rails/logger.rb | 5 +++-- spec/logtail-rails/logger_spec.rb | 33 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/lib/logtail-rails/logger.rb b/lib/logtail-rails/logger.rb index ffbe499..28bbc01 100755 --- a/lib/logtail-rails/logger.rb +++ b/lib/logtail-rails/logger.rb @@ -17,11 +17,12 @@ class Logger < ::Logger end # Logtail::Logger also works as ActiveSupport::BroadcastLogger - def is_a?(clazz) - return true if clazz == ::ActiveSupport::BroadcastLogger + def kind_of?(clazz) + return true if defined?(::ActiveSupport::BroadcastLogger) && clazz == ::ActiveSupport::BroadcastLogger super(clazz) end + alias is_a? kind_of? def broadcasts [self] + @extra_loggers diff --git a/spec/logtail-rails/logger_spec.rb b/spec/logtail-rails/logger_spec.rb index 9705b69..d440f84 100755 --- a/spec/logtail-rails/logger_spec.rb +++ b/spec/logtail-rails/logger_spec.rb @@ -40,5 +40,38 @@ expect(io.string).to include(',"public_info":"public_info",') expect(io.string).to include(',"north_pole":{"secret":"[FILTERED]"},') end + + it "should be considered a broadcast logger in Rails 7.1" do + expect(logger.is_a?(::Logger)).to eq(true) + expect(logger).to be_kind_of(::Logger) + + expect(logger.is_a?(::Logtail::Logger)).to eq(true) + expect(logger).to be_kind_of(::Logtail::Logger) + + if Rails::VERSION::STRING >= "7.1" + expect(logger.is_a?(::ActiveSupport::BroadcastLogger)).to eq(true) + expect(logger).to be_kind_of(::ActiveSupport::BroadcastLogger) + end + + expect(logger.is_a?(::Rails::Application)).to eq(false) + expect(logger).to_not be_kind_of(::Rails::Application) + end + + it "should be able to start and stop broadcast" do + expect(::ActiveSupport::Logger.logger_outputs_to?(logger, STDOUT)).to eq(false) + expect(logger.broadcasts.length).to eq(1) + + logger.broadcast_to(STDOUT) + + # Logger.logger_outputs_to? didn't work correctly for broadcasting loggers before, skip the assert + # see https://github.com/rails/rails/pull/49417/files#r1340736648 + expect(::ActiveSupport::Logger.logger_outputs_to?(logger, STDOUT)).to eq(true) if Rails::VERSION::STRING >= "7.1" + expect(logger.broadcasts.length).to eq(2) + + logger.stop_broadcasting_to(STDOUT) + + expect(::ActiveSupport::Logger.logger_outputs_to?(logger, STDOUT)).to eq(false) + expect(logger.broadcasts.length).to eq(1) + end end end