Skip to content

Commit

Permalink
Duplicate Rails.logger before assigning it to the SDK (#2086)
Browse files Browse the repository at this point in the history
* Duplicate `Rails.logger` before assigning it to the SDK

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.

* Update changelog
  • Loading branch information
st0012 authored Aug 9, 2023
1 parent bc0f8ce commit 8abb2d2
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
config.trace_propagation_targets = [/example.com/, 'foobar.org/api/v2']
```

### Bug Fixes

- Duplicate `Rails.logger` before assigning it to the SDK ([#2086](https://github.com/getsentry/sentry-ruby/pull/2086))

## 5.10.0

### Features
Expand Down
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
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 8abb2d2

Please sign in to comment.