Skip to content

Commit 6049bfd

Browse files
authored
fix(rails): properly skip silenced logs from AR (#2775)
1 parent 98f97b4 commit 6049bfd

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
- Optimize getting query source location in ActiveRecord tracing - this makes tracing up to roughly 40-60% faster depending on the use cases ([#2769](https://github.com/getsentry/sentry-ruby/pull/2769))
66

7+
### Bug fixes
8+
9+
- Properly skip silenced `ActiveRecord::Base.logger`'s log entries in the ActiveRecord log subscriber ([#2775](https://github.com/getsentry/sentry-ruby/pull/2775))
10+
711
## 6.1.0
812

913
### Features

sentry-rails/lib/sentry/rails/log_subscribers/active_record_subscriber.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class ActiveRecordSubscriber < Sentry::Rails::LogSubscriber
3030
# @param event [ActiveSupport::Notifications::Event] The SQL event
3131
def sql(event)
3232
return unless Sentry.initialized?
33+
return if logger && !logger.info?
3334
return if EXCLUDED_NAMES.include?(event.payload[:name])
3435

3536
sql = event.payload[:sql]

sentry-rails/spec/dummy/test_rails_app/config/application.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
require "action_cable/engine"
1010
require "active_storage/engine"
1111

12-
ActiveRecord::Base.logger = Logger.new(nil)
13-
1412
module Sentry
1513
module Rails
1614
module Test
@@ -83,7 +81,6 @@ def self.load_test_schema
8381
# This can be inherited and extended by subclasses
8482
def configure
8583
config.root = Test::Application.root_path
86-
config.logger = ActiveSupport::Logger.new(nil)
8784
config.active_support.deprecation = :silence
8885
config.hosts = nil
8986
config.secret_key_base = "test 123"

sentry-rails/spec/sentry/rails/log_subscribers/active_record_subscriber_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,36 @@
229229
end
230230
end
231231

232+
context "when logger is silenced" do
233+
before do
234+
make_basic_app do |config, app|
235+
config.enable_logs = true
236+
237+
config.rails.structured_logging.enabled = true
238+
config.rails.structured_logging.subscribers = {
239+
active_record: Sentry::Rails::LogSubscribers::ActiveRecordSubscriber
240+
}
241+
end
242+
end
243+
244+
it "does not log silenced logs", if: RAILS_VERSION > 5.2 do
245+
# This makes it log only errors
246+
ActiveRecord::Base.logger.silence { Post.create! }
247+
248+
Post.find(1)
249+
250+
Sentry.get_current_client.flush
251+
252+
expect(
253+
sentry_logs.find { |log| log[:body].include?("Database query: Post Create") }
254+
).to be(nil)
255+
256+
expect(
257+
sentry_logs.find { |log| log[:body].include?("Database query: Post Load") }
258+
).to_not be(nil)
259+
end
260+
end
261+
232262
context "when logging is disabled" do
233263
before do
234264
make_basic_app do |config|

0 commit comments

Comments
 (0)