diff options
author | lsylvester <lachlan.sylvester@hypothetical.com.au> | 2018-07-28 23:50:03 +1000 |
---|---|---|
committer | Kasper Timm Hansen <kaspth@gmail.com> | 2018-07-28 15:50:03 +0200 |
commit | 845cbb4bb2869444379354a87f83553faaadc961 (patch) | |
tree | ace7f077dfce904a6a03f92a0ccfb2b91fd9b883 | |
parent | 45903be1344992b1f0156cd88c351067bdc93726 (diff) | |
download | rails-845cbb4bb2869444379354a87f83553faaadc961.tar.gz rails-845cbb4bb2869444379354a87f83553faaadc961.tar.bz2 rails-845cbb4bb2869444379354a87f83553faaadc961.zip |
Avoid logging ActiveRecord::LogSubscriber as the query source when the source is ignored (#33455)
-rw-r--r-- | activerecord/lib/active_record/log_subscriber.rb | 21 | ||||
-rw-r--r-- | activerecord/test/cases/log_subscriber_test.rb | 13 |
2 files changed, 22 insertions, 12 deletions
diff --git a/activerecord/lib/active_record/log_subscriber.rb b/activerecord/lib/active_record/log_subscriber.rb index cf884bc6da..51ae1fdb45 100644 --- a/activerecord/lib/active_record/log_subscriber.rb +++ b/activerecord/lib/active_record/log_subscriber.rb @@ -100,9 +100,10 @@ module ActiveRecord end def log_query_source - source_line, line_number = extract_callstack(caller_locations) + line = extract_callstack(caller_locations) - if source_line + if line + source_line, line_number = line.path, line.lineno if defined?(::Rails.root) app_root = "#{::Rails.root}/" source_line = source_line.sub(app_root, "") @@ -113,23 +114,19 @@ module ActiveRecord end def extract_callstack(callstack) - line = callstack.find do |frame| + callstack.find do |frame| frame.absolute_path && !ignored_callstack(frame.absolute_path) end - - offending_line = line || callstack.first - - [ - offending_line.path, - offending_line.lineno - ] end RAILS_GEM_ROOT = File.expand_path("../../..", __dir__) + "/" + class_attribute :ignored_callstack_paths, default: [RAILS_GEM_ROOT, RbConfig::CONFIG["rubylibdir"]] + def ignored_callstack(path) - path.start_with?(RAILS_GEM_ROOT) || - path.start_with?(RbConfig::CONFIG["rubylibdir"]) + ignored_callstack_paths.any? do |ignored_path| + path.start_with?(ignored_path) + end end end end diff --git a/activerecord/test/cases/log_subscriber_test.rb b/activerecord/test/cases/log_subscriber_test.rb index e2742ed33e..3f26eaa88d 100644 --- a/activerecord/test/cases/log_subscriber_test.rb +++ b/activerecord/test/cases/log_subscriber_test.rb @@ -177,11 +177,24 @@ class LogSubscriberTest < ActiveRecord::TestCase logger = TestDebugLogSubscriber.new logger.sql(Event.new(0, sql: "hi mom!")) + assert_equal 2, @logger.logged(:debug).size assert_match(/↳/, @logger.logged(:debug).last) ensure ActiveRecord::Base.verbose_query_logs = false end + def test_verbose_query_with_ignored_callstack + ActiveRecord::Base.verbose_query_logs = true + ActiveRecord::LogSubscriber.ignored_callstack_paths.push("/") + logger = TestDebugLogSubscriber.new + logger.sql(Event.new(0, sql: "hi mom!")) + assert_equal 1, @logger.logged(:debug).size + assert_no_match(/↳/, @logger.logged(:debug).last) + ensure + ActiveRecord::LogSubscriber.ignored_callstack_paths.delete("/") + ActiveRecord::Base.verbose_query_logs = false + end + def test_verbose_query_logs_disabled_by_default logger = TestDebugLogSubscriber.new logger.sql(Event.new(0, sql: "hi mom!")) |