Skip to content

Commit

Permalink
Duplicate Rails.logger before assigning it to the SDK
Browse files Browse the repository at this point in the history
If we don't duplicate it, logger configuration made to the SDK's logger
will be applied to the Rails logger as well.

For example, in an Rails app:

```rb
Sentry.init do |config|
  config.logger.level = Logger::WARN
end
```

Will make the Rails logger's level to be `Logger::WARN` as well.
  • Loading branch information
st0012 committed Aug 6, 2023
1 parent bc0f8ce commit 91098f9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion sentry-rails/lib/sentry/rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Configuration
@excluded_exceptions = @excluded_exceptions.concat(Sentry::Rails::IGNORE_DEFAULT)

if ::Rails.logger
@logger = ::Rails.logger
@logger = ::Rails.logger.dup

Check warning on line 15 in sentry-rails/lib/sentry/rails/configuration.rb

View check run for this annotation

Codecov / codecov/patch

sentry-rails/lib/sentry/rails/configuration.rb#L15

Added line #L15 was not covered by tests
else
@logger.warn(Sentry::LOGGER_PROGNAME) do
<<~MSG
Expand Down
2 changes: 1 addition & 1 deletion sentry-rails/spec/dummy/test_rails_app/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def self.name
app.config.action_controller.view_paths = "spec/dummy/test_rails_app"
app.config.hosts = nil
app.config.secret_key_base = "test"
app.config.logger = Logger.new(nil)
app.config.logger = ActiveSupport::Logger.new(nil)
app.config.eager_load = true
app.config.active_job.queue_adapter = :test

Expand Down
8 changes: 6 additions & 2 deletions sentry-rails/spec/sentry/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@
end

describe "logger detection" do
it "sets Sentry.configuration.logger correctly" do
expect(Sentry.configuration.logger).to eq(Rails.logger)
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)
end

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

0 comments on commit 91098f9

Please sign in to comment.