Skip to content

Commit

Permalink
L-873 Fix compatibility with Rails < 7.1 (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrHeinz authored Oct 27, 2023
1 parent 21f750c commit caed93e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/logtail-rails/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions spec/logtail-rails/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit caed93e

Please sign in to comment.