Repository: rails/rails
File: actionpack/lib/action_dispatch/railtie.rb (Ruby)

Code before:
      unless app.config.action_dispatch.domain_extractor.nil?
        ActionDispatch::Http::URL.domain_extractor = app.config.action_dispatch.domain_extractor
      end

      ActionDispatch::ParamBuilder.ignore_leading_brackets = app.config.action_dispatch.ignore_leading_brackets
      ActionDispatch::QueryParser.strict_query_string_separator = app.config.action_dispatch.strict_query_string_separator

      ActionDispatch.verbose_redirect_logs = app.config.action_dispatch.verbose_redirect_logs

      ActiveSupport.on_load(:action_dispatch_request) do
        self.ignore_accept_header = app.config.action_dispatch.ignore_accept_header
        ActionDispatch::Request::Utils.perform_deep_munge = app.config.action_dispatch.perform_deep_munge
      end

      ActiveSupport.on_load(:action_dispatch_response) do
        self.default_charset = app.config.action_dispatch.default_charset || app.config.encoding
        self.default_headers = app.config.action_dispatch.default_headers
      end

      ActionDispatch::ExceptionWrapper.rescue_responses.merge!(config.action_dispatch.rescue_responses)
      ActionDispatch::ExceptionWrapper.rescue_templates.merge!(config.action_dispatch.rescue_templates)

      config.action_dispatch.always_write_cookie = Rails.env.development? if config.action_dispatch.always_write_cookie.nil?
      ActionDispatch::Cookies::CookieJar.always_write_cookie = config.action_dispatch.always_write_cookie

      ActionDispatch::Routing::Mapper.route_source_locations = Rails.env.development? || ActionDispatch.verbose_redirect_logs

      ActionDispatch::Http::Cache::Request.strict_freshness = app.config.action_dispatch.strict_freshness
      ActionDispatch.test_app = app
    end
  end
end


Review comment (performance):
I dislike having to lean on this feature. I made a patch to use backtrace cleaners in both places. Let's use this instead:
```diff
diff --git a/actionpack/lib/action_controller/log_subscriber.rb b/actionpack/lib/action_controller/log_subscriber.rb
index d98ca3ef3d..3eb053df31 100644
--- a/actionpack/lib/action_controller/log_subscriber.rb
+++ b/actionpack/lib/action_controller/log_subscriber.rb
@@ -67,12 +67,8 @@ def redirect_to(event)
       info { "Redirected to #{event.payload[:location]}" }
 
       info do
-        if ActionDispatch.verbose_redirect_logs
-          source = redirect_source_location
-
-          if source
-            "↳ #{source}"
-          end
+        if ActionDispatch.verbose_redirect_logs && (source = redirect_source_location)
+          "↳ #{source}"
         end
       end
     end
diff --git a/actionpack/lib/action_controller/railtie.rb b/actionpack/lib/action_controller/railtie.rb
index 9e4b2d9909..756295c32f 100644
--- a/actionpack/lib/action_controller/railtie.rb
+++ b/actionpack/lib/action_controller/railtie.rb
@@ -137,9 +137,7 @@ class Railtie < Rails::Railtie # :nodoc:
     end
 
     initializer "action_controller.backtrace_cleaner" do
-      ActiveSupport.on_load(:action_controller) do
-        ActionController::LogSubscriber.backtrace_cleaner = ::Rails.backtrace_cleaner
-      end
+      ActionController::LogSubscriber.backtrace_cleaner = Rails.backtrace_cleaner
     end
   end
 end
diff --git a/actionpack/lib/action_dispatch/log_subscriber.rb b/actionpack/lib/action_dispatch/log_subscriber.rb
index aaa63d3b95..bcb040bfd9 100644
--- a/actionpack/lib/action_dispatch/log_subscriber.rb
+++ b/actionpack/lib/action_dispatch/log_subscriber.rb
@@ -4,23 +4,17 @@
 
 module ActionDispatch
   class LogSubscriber < ActiveSupport::LogSubscriber
+    class_attribute :backtrace_cleaner, default: ActiveSupport::BacktraceCleaner.new
+
     def redirect(event)
       payload = event.payload
 
       info { "Redirected to #{payload[:location]}" }
 
       info do
-        message = nil
-
-        if ActionDispatch.verbose_redirect_logs
-          request = payload[:request]
-
-          request.routes.router.recognize(request) do |route|
-            message = "↳ #{route.source_location}"
-          end
+        if ActionDispatch.verbose_redirect_logs && (source = redirect_source_location)
+          "↳ #{source}"
         end
-
-        message
       end
 
       info do
@@ -33,6 +27,12 @@ def redirect(event)
       end
     end
     subscribe_log_level :redirect, :info
+
+    private
+
+     def redirect_source_location
+      backtrace_cleaner.first_clean_frame
+    end
   end
 end
 
diff --git a/actionpack/lib/action_dispatch/railtie.rb b/actionpack/lib/action_dispatch/railtie.rb
index e3ecacc9d6..23e87566ed 100644
--- a/actionpack/lib/action_dispatch/railtie.rb
+++ b/actionpack/lib/action_dispatch/railtie.rb
@@ -81,10 +81,14 @@ class Railtie < Rails::Railtie # :nodoc:
       config.action_dispatch.always_write_cookie = Rails.env.development? if config.action_dispatch.always_write_cookie.nil?
       ActionDispatch::Cookies::CookieJar.always_write_cookie = config.action_dispatch.always_write_cookie
 
-      ActionDispatch::Routing::Mapper.route_source_locations = Rails.env.development? || ActionDispatch.verbose_redirect_logs
+      ActionDispatch::Routing::Mapper.route_source_locations = Rails.env.development?
 
       ActionDispatch::Http::Cache::Request.strict_freshness = app.config.action_dispatch.strict_freshness
       ActionDispatch.test_app = app
     end
+
+    initializer "action_dispatch.backtrace_cleaner" do
+      ActionDispatch::LogSubscriber.backtrace_cleaner = Rails.backtrace_cleaner
+    end
   end
 end
diff --git a/actionpack/test/controller/log_subscriber_test.rb b/actionpack/test/controller/log_subscriber_test.rb
index 6dd6ae6207..c208d5e5dc 100644
--- a/actionpack/test/controller/log_subscriber_test.rb
+++ b/actionpack/test/controller/log_subscriber_test.rb
@@ -318,15 +318,19 @@ def test_filter_redirect_bad_uri
   end
 
   def test_verbose_redirect_logs
+    old_cleaner = ActionController::LogSubscriber.backtrace_cleaner
+    ActionController::LogSubscriber.backtrace_cleaner = ActionController::LogSubscriber.backtrace_cleaner.dup
+    ActionController::LogSubscriber.backtrace_cleaner.add_silencer { |location| !location.include?(__FILE__) }
     ActionDispatch.verbose_redirect_logs = true
 
     get :redirector
     wait
 
     assert_equal 4, logs.size
-    assert_match(/↳/, logs[2])
+    assert_match(/↳ #{__FILE__}/, logs[2])
   ensure
     ActionDispatch.verbose_redirect_logs = false
+    ActionController::LogSubscriber.backtrace_cleaner = old_cleaner
   end
 
   def test_send_data
diff --git a/actionpack/test/dispatch/routing/log_subscriber_test.rb b/actionpack/test/dispatch/routing/log_subscriber_test.rb
index b44ecf66d9..2c8e0253f6 100644
--- a/actionpack/test/dispatch/routing/log_subscriber_test.rb
+++ b/actionpack/test/dispatch/routing/log_subscriber_test.rb
@@ -26,6 +26,9 @@ def setup
   end
 
   test "verbose redirect logs" do
+    old_cleaner = ActionDispatch::LogSubscriber.backtrace_cleaner
+    ActionDispatch::LogSubscriber.backtrace_cleaner = ActionDispatch::LogSubscriber.backtrace_cleaner.dup
+    ActionDispatch::LogSubscriber.backtrace_cleaner.add_silencer { |location| !location.include?(__FILE__) }
     ActionDispatch.verbose_redirect_logs = true
 
     draw do
@@ -36,9 +39,10 @@ def setup
     wait
 
     assert_equal 3, logs.size
-    assert_match(/↳/, logs[1])
+    assert_match(/↳ #{__FILE__}/, logs[1])
   ensure
     ActionDispatch.verbose_redirect_logs = false
+    ActionDispatch::LogSubscriber.backtrace_cleaner = old_cleaner
   end
 
   private

```

Code after:

      unless app.config.action_dispatch.domain_extractor.nil?
        ActionDispatch::Http::URL.domain_extractor = app.config.action_dispatch.domain_extractor
      end

      unless app.config.action_dispatch.ignore_leading_brackets.nil?
        ActionDispatch::ParamBuilder.ignore_leading_brackets = app.config.action_dispatch.ignore_leading_brackets
      end
      unless app.config.action_dispatch.strict_query_string_separator.nil?
        ActionDispatch::QueryParser.strict_query_string_separator = app.config.action_dispatch.strict_query_string_separator
      end

      ActionDispatch.verbose_redirect_logs = app.config.action_dispatch.verbose_redirect_logs

      ActiveSupport.on_load(:action_dispatch_request) do
        self.ignore_accept_header = app.config.action_dispatch.ignore_accept_header
        ActionDispatch::Request::Utils.perform_deep_munge = app.config.action_dispatch.perform_deep_munge
      end

      ActiveSupport.on_load(:action_dispatch_response) do
        self.default_charset = app.config.action_dispatch.default_charset || app.config.encoding
        self.default_headers = app.config.action_dispatch.default_headers
      end

      ActionDispatch::ExceptionWrapper.rescue_responses.merge!(config.action_dispatch.rescue_responses)
      ActionDispatch::ExceptionWrapper.rescue_templates.merge!(config.action_dispatch.rescue_templates)

      config.action_dispatch.always_write_cookie = Rails.env.development? if config.action_dispatch.always_write_cookie.nil?
      ActionDispatch::Cookies::CookieJar.always_write_cookie = config.action_dispatch.always_write_cookie

      ActionDispatch::Routing::Mapper.route_source_locations = Rails.env.development?

      ActionDispatch::Http::Cache::Request.strict_freshness = app.config.action_dispatch.strict_freshness
      ActionDispatch.test_app = app
    end

    initializer "action_dispatch.backtrace_cleaner" do
      ActionDispatch::LogSubscriber.backtrace_cleaner = Rails.backtrace_cleaner
    end
  end
end