Skip to content

Commit

Permalink
Adopt Rails 7.1's new BroadcastLogger
Browse files Browse the repository at this point in the history
In rails/rails#48615, Rails 7.1 introduced a new
BroadcastLogger class that allows users to send logs to multiple loggers,
which means we need to adjust the SDK's logger assignment for it.
  • Loading branch information
st0012 committed Oct 1, 2023
1 parent 9f3567c commit 5f657ae
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion sentry-rails/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/doc/
/pkg/
/spec/reports/
/spec/dummy/test_rails_app/db
/spec/dummy/test_rails_app/db*
/tmp/

# rspec failure tracking
Expand Down
7 changes: 6 additions & 1 deletion sentry-rails/lib/sentry/rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ class Configuration
@excluded_exceptions = @excluded_exceptions.concat(Sentry::Rails::IGNORE_DEFAULT)

if ::Rails.logger
@logger = ::Rails.logger.dup
if ::Rails.logger.respond_to?(:broadcasts)
dupped_broadcasts = ::Rails.logger.broadcasts.map(&:dup)
@logger = ::ActiveSupport::BroadcastLogger.new(*dupped_broadcasts)
else
@logger = ::Rails.logger.dup
end
else
@logger.warn(Sentry::LOGGER_PROGNAME) do
<<~MSG
Expand Down
22 changes: 17 additions & 5 deletions sentry-rails/spec/sentry/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,23 @@

describe "logger detection" do
it "sets a duplicated Rails logger as the SDK's logger" do
expect(Sentry.configuration.logger).to be_a(ActiveSupport::Logger)
Sentry.configuration.logger.level = ::Logger::WARN
# Configuring the SDK's logger should not affect the Rails logger
expect(Rails.logger.level).to eq(::Logger::DEBUG)
expect(Sentry.configuration.logger.level).to eq(::Logger::WARN)
if Gem::Version.new(Rails.version) > Gem::Version.new("7.0.0")
expect(Sentry.configuration.logger).to be_a(ActiveSupport::BroadcastLogger)

Sentry.configuration.logger.level = ::Logger::WARN

# Configuring the SDK's logger should not affect the Rails logger
expect(Rails.logger.broadcasts.first.level).to eq(::Logger::DEBUG)
expect(Sentry.configuration.logger.level).to eq(::Logger::WARN)
else
expect(Sentry.configuration.logger).to be_a(ActiveSupport::Logger)

Sentry.configuration.logger.level = ::Logger::WARN

# Configuring the SDK's logger should not affect the Rails logger
expect(Rails.logger.level).to eq(::Logger::DEBUG)
expect(Sentry.configuration.logger.level).to eq(::Logger::WARN)
end
end

it "respects the logger set by user" do
Expand Down

0 comments on commit 5f657ae

Please sign in to comment.