diff --git a/CHANGELOG.md b/CHANGELOG.md index 327242a8f..c5393a8b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ - 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)) +### Bug fixes + +- Properly skip silenced `ActiveRecord::Base.logger`'s log entries in the ActiveRecord log subscriber ([#2775](https://github.com/getsentry/sentry-ruby/pull/2775)) + ## 6.1.0 ### Features diff --git a/sentry-rails/lib/sentry/rails/log_subscribers/active_record_subscriber.rb b/sentry-rails/lib/sentry/rails/log_subscribers/active_record_subscriber.rb index e6a2aeb9e..3bc30960f 100644 --- a/sentry-rails/lib/sentry/rails/log_subscribers/active_record_subscriber.rb +++ b/sentry-rails/lib/sentry/rails/log_subscribers/active_record_subscriber.rb @@ -30,6 +30,7 @@ class ActiveRecordSubscriber < Sentry::Rails::LogSubscriber # @param event [ActiveSupport::Notifications::Event] The SQL event def sql(event) return unless Sentry.initialized? + return if logger && !logger.info? return if EXCLUDED_NAMES.include?(event.payload[:name]) sql = event.payload[:sql] diff --git a/sentry-rails/spec/dummy/test_rails_app/config/application.rb b/sentry-rails/spec/dummy/test_rails_app/config/application.rb index 02a5756e7..b8c43b5c3 100644 --- a/sentry-rails/spec/dummy/test_rails_app/config/application.rb +++ b/sentry-rails/spec/dummy/test_rails_app/config/application.rb @@ -9,8 +9,6 @@ require "action_cable/engine" require "active_storage/engine" -ActiveRecord::Base.logger = Logger.new(nil) - module Sentry module Rails module Test @@ -83,7 +81,6 @@ def self.load_test_schema # This can be inherited and extended by subclasses def configure config.root = Test::Application.root_path - config.logger = ActiveSupport::Logger.new(nil) config.active_support.deprecation = :silence config.hosts = nil config.secret_key_base = "test 123" diff --git a/sentry-rails/spec/sentry/rails/log_subscribers/active_record_subscriber_spec.rb b/sentry-rails/spec/sentry/rails/log_subscribers/active_record_subscriber_spec.rb index 33625cada..0dd70ede1 100644 --- a/sentry-rails/spec/sentry/rails/log_subscribers/active_record_subscriber_spec.rb +++ b/sentry-rails/spec/sentry/rails/log_subscribers/active_record_subscriber_spec.rb @@ -229,6 +229,36 @@ end end + context "when logger is silenced" do + before do + make_basic_app do |config, app| + config.enable_logs = true + + config.rails.structured_logging.enabled = true + config.rails.structured_logging.subscribers = { + active_record: Sentry::Rails::LogSubscribers::ActiveRecordSubscriber + } + end + end + + it "does not log silenced logs", if: RAILS_VERSION > 5.2 do + # This makes it log only errors + ActiveRecord::Base.logger.silence { Post.create! } + + Post.find(1) + + Sentry.get_current_client.flush + + expect( + sentry_logs.find { |log| log[:body].include?("Database query: Post Create") } + ).to be(nil) + + expect( + sentry_logs.find { |log| log[:body].include?("Database query: Post Load") } + ).to_not be(nil) + end + end + context "when logging is disabled" do before do make_basic_app do |config|