Skip to content

Commit

Permalink
Adopt Rails 7.1's new BroadcastLogger (#2120)
Browse files Browse the repository at this point in the history
* Adopt Rails 7.1's new BroadcastLogger

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.

* Update changelog
  • Loading branch information
st0012 committed Oct 2, 2023
1 parent 9f3567c commit ab9fd5a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Features

- Record client reports for profiles [#2107](https://github.com/getsentry/sentry-ruby/pull/2107)
- Adopt Rails 7.1's new BroadcastLogger [#2120](https://github.com/getsentry/sentry-ruby/pull/2120)

### Bug Fixes

Expand Down
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
29 changes: 24 additions & 5 deletions sentry-rails/spec/sentry/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
make_basic_app
end

after do
# We need to cleanup Rails.logger because after https://github.com/rails/rails/pull/49417
# Rails.logger could get recreated with the previous logger value and become deeply nested broadcast logger
Rails.logger = nil
end

it "has version set" do
expect(described_class::VERSION).to be_a(String)
end
Expand All @@ -36,11 +42,24 @@

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.1.0.beta")
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).to be_a(ActiveSupport::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 ab9fd5a

Please sign in to comment.