From c675783eb4fb42a357ec068d4cfd83bf08f78aea Mon Sep 17 00:00:00 2001
From: Lachlan Sylvester <lachlan.sylvester@publicisfrontfoot.com.au>
Date: Mon, 6 Aug 2018 11:06:01 +1000
Subject: Use backtrace cleaner to clean up backtrace for verbose query logs

---
 activerecord/lib/active_record/log_subscriber.rb | 14 +++++---------
 activerecord/lib/active_record/railtie.rb        |  4 ++++
 2 files changed, 9 insertions(+), 9 deletions(-)

(limited to 'activerecord/lib')

diff --git a/activerecord/lib/active_record/log_subscriber.rb b/activerecord/lib/active_record/log_subscriber.rb
index 1ae6840921..6b84431343 100644
--- a/activerecord/lib/active_record/log_subscriber.rb
+++ b/activerecord/lib/active_record/log_subscriber.rb
@@ -4,6 +4,8 @@ module ActiveRecord
   class LogSubscriber < ActiveSupport::LogSubscriber
     IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"]
 
+    class_attribute :backtrace_cleaner, default: ActiveSupport::BacktraceCleaner.new
+
     def self.runtime=(value)
       ActiveRecord::RuntimeRegistry.sql_runtime = value
     end
@@ -100,21 +102,15 @@ module ActiveRecord
       end
 
       def log_query_source
-        location = extract_query_source_location(caller_locations)
-
-        if location
-          source = "#{location.path}:#{location.lineno}"
-          source = source.sub("#{::Rails.root}/", "") if defined?(::Rails.root)
+        source = extract_query_source_location(caller)
 
+        if source
           logger.debug("  ↳ #{source}")
         end
       end
 
-      RAILS_GEM_ROOT  = File.expand_path("../../..", __dir__) + "/"
-      PATHS_TO_IGNORE = /\A(#{RAILS_GEM_ROOT}|#{RbConfig::CONFIG["rubylibdir"]})/
-
       def extract_query_source_location(locations)
-        locations.find { |line| line.absolute_path && !line.absolute_path.match?(PATHS_TO_IGNORE) }
+        backtrace_cleaner.clean(locations).first
       end
   end
 end
diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb
index 009f412234..7ece083fd4 100644
--- a/activerecord/lib/active_record/railtie.rb
+++ b/activerecord/lib/active_record/railtie.rb
@@ -77,6 +77,10 @@ module ActiveRecord
       ActiveSupport.on_load(:active_record) { self.logger ||= ::Rails.logger }
     end
 
+    initializer "active_record.backtrace_cleaner" do
+      ActiveSupport.on_load(:active_record) { LogSubscriber.backtrace_cleaner = ::Rails.backtrace_cleaner }
+    end
+
     initializer "active_record.migration_error" do
       if config.active_record.delete(:migration_error) == :page_load
         config.app_middleware.insert_after ::ActionDispatch::Callbacks,
-- 
cgit v1.2.3