Skip to content

Commit a393e0d

Browse files
authored
Merge pull request rails#43755 from djfpaagman/log_route_redirects
Log redirects from router similarly to controller redirects
2 parents 9b138de + ee47002 commit a393e0d

File tree

5 files changed

+76
-4
lines changed

5 files changed

+76
-4
lines changed

actionpack/CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
* Prevent `ActionDispatch::ServerTiming` from overwriting existing values in `Server-Timing`.
1+
* Log redirects from routes the same way as redirects from controllers.
2+
3+
*Dennis Paagman*
24

5+
* Prevent `ActionDispatch::ServerTiming` from overwriting existing values in `Server-Timing`.
36
Previously, if another middleware down the chain set `Server-Timing` header,
47
it would overwritten by `ActionDispatch::ServerTiming`.
58

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
module ActionDispatch
4+
class LogSubscriber < ActiveSupport::LogSubscriber
5+
def redirect(event)
6+
payload = event.payload
7+
8+
info { "Redirected to #{payload[:location]}" }
9+
10+
info do
11+
status = payload[:status]
12+
13+
message = +"Completed #{status} #{Rack::Utils::HTTP_STATUS_CODES[status]} in #{event.duration.round}ms"
14+
message << "\n\n" if defined?(Rails.env) && Rails.env.development?
15+
16+
message
17+
end
18+
end
19+
end
20+
end
21+
22+
ActionDispatch::LogSubscriber.attach_to :action_dispatch

actionpack/lib/action_dispatch/railtie.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "action_dispatch"
4+
require "action_dispatch/log_subscriber"
45
require "active_support/messages/rotation_configuration"
56

67
module ActionDispatch

actionpack/lib/action_dispatch/routing/redirection.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,17 @@ def initialize(status, block)
1818
def redirect?; true; end
1919

2020
def call(env)
21-
serve Request.new env
21+
ActiveSupport::Notifications.instrument("redirect.action_dispatch") do |payload|
22+
response = build_response(Request.new(env))
23+
24+
payload[:status] = @status
25+
payload[:location] = response.headers["Location"]
26+
27+
response.to_a
28+
end
2229
end
2330

24-
def serve(req)
31+
def build_response(req)
2532
uri = URI.parse(path(req.path_parameters, req))
2633

2734
unless uri.host
@@ -46,7 +53,7 @@ def serve(req)
4653
"Content-Length" => body.length.to_s
4754
}
4855

49-
[ status, headers, [body] ]
56+
ActionDispatch::Response.new(status, headers, body)
5057
end
5158

5259
def path(params, request)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
require "abstract_unit"
4+
require "active_support/log_subscriber/test_helper"
5+
require "action_dispatch/log_subscriber"
6+
7+
class TestLogSubscriber < ActionDispatch::IntegrationTest
8+
include ActiveSupport::LogSubscriber::TestHelper
9+
10+
def setup
11+
super
12+
ActionDispatch::LogSubscriber.attach_to :action_dispatch
13+
end
14+
15+
def test_redirect_logging
16+
draw do
17+
get "redirect", to: redirect("/login")
18+
end
19+
20+
get "/redirect"
21+
wait
22+
assert_equal 2, logs.size
23+
assert_equal "Redirected to http://www.example.com/login", logs.first
24+
assert_match(/Completed 301/, logs.last)
25+
end
26+
27+
private
28+
def draw(&block)
29+
self.class.stub_controllers do |routes|
30+
routes.default_url_options = { host: "www.example.com" }
31+
routes.draw(&block)
32+
@app = RoutedRackApp.new routes
33+
end
34+
end
35+
36+
def logs
37+
@logs ||= @logger.logged(:info)
38+
end
39+
end

0 commit comments

Comments
 (0)