Skip to content

Commit cd053bc

Browse files
authored
Merge pull request rails#50992 from KJTsanaktsidis/ktsanaktsidis/persist_logger_tags_until_as_event
Don't pop logger tags in Rails::Rack::Logger until request is finished
2 parents 5c0286d + 5faeb70 commit cd053bc

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

railties/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Ensure logger tags configured with `config.log_tags` are still active in `request.action_dispatch` handlers
2+
3+
*KJ Tsanaktsidis*
4+
15
* Setup jemalloc in the default Dockerfile for memory optimization.
26

37
*Matt Almeida*, *Jean Boussier*

railties/lib/rails/rack/logger.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,23 @@ def initialize(app, taggers = nil)
2020
def call(env)
2121
request = ActionDispatch::Request.new(env)
2222

23-
if logger.respond_to?(:tagged)
24-
logger.tagged(*compute_tags(request)) { call_app(request, env) }
23+
logger_tag_pop_count = if logger.respond_to?(:push_tags)
24+
logger.push_tags(*compute_tags(request)).size
2525
else
26-
call_app(request, env)
26+
0
2727
end
28+
call_app(request, env, logger_tag_pop_count)
2829
end
2930

3031
private
31-
def call_app(request, env) # :doc:
32+
def call_app(request, env, logger_tag_pop_count) # :doc:
3233
instrumenter = ActiveSupport::Notifications.instrumenter
3334
handle = instrumenter.build_handle("request.action_dispatch", { request: request })
3435
handle.start
3536

3637
logger.info { started_request_message(request) }
3738
status, headers, body = response = @app.call(env)
38-
body = ::Rack::BodyProxy.new(body) { finish_request_instrumentation(handle) }
39+
body = ::Rack::BodyProxy.new(body) { finish_request_instrumentation(handle, logger_tag_pop_count) }
3940

4041
if response.frozen?
4142
[status, headers, body]
@@ -44,7 +45,7 @@ def call_app(request, env) # :doc:
4445
response
4546
end
4647
rescue Exception
47-
finish_request_instrumentation(handle)
48+
finish_request_instrumentation(handle, logger_tag_pop_count)
4849
raise
4950
end
5051

@@ -74,8 +75,9 @@ def logger
7475
Rails.logger
7576
end
7677

77-
def finish_request_instrumentation(handle)
78+
def finish_request_instrumentation(handle, logger_tag_pop_count)
7879
handle.finish
80+
logger.pop_tags(logger_tag_pop_count) if logger.respond_to?(:pop_tags) && logger_tag_pop_count > 0
7981
ActiveSupport::LogSubscriber.flush_all!
8082
end
8183
end

railties/test/rack_logger_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,32 @@ def test_logger_is_flushed_after_request_finished
119119
ensure
120120
@notifier.unsubscribe block_sub
121121
end
122+
123+
def test_logger_pushes_tags
124+
@logger = ActiveSupport::TaggedLogging.new(@logger)
125+
set_logger(@logger)
126+
127+
taggers = ["tag1", ->(_req) { "tag2" }]
128+
logger_middleware = TestLogger.new(@logger, taggers: taggers) do
129+
# We can't really assert on logging something with the tags, because the MockLogger implementation
130+
# does not call the formatter (which is responsible for appending the tags)
131+
assert_equal(["tag1", "tag2"], @logger.formatter.current_tags)
132+
end
133+
block_sub = @notifier.subscribe "request.action_dispatch" do |_event|
134+
assert_equal(["tag1", "tag2"], @logger.formatter.current_tags)
135+
end
136+
137+
# Call the app - it should log the inside app message
138+
response_body = logger_middleware.call("REQUEST_METHOD" => "GET").last
139+
# The tags should still be open as long as the request body isn't closed
140+
assert_equal(["tag1", "tag2"], @logger.formatter.current_tags)
141+
# And now should fire the request.action_dispatch event and call the event handler
142+
response_body.close
143+
# And it should also clear the tag stack.
144+
assert_equal([], @logger.formatter.current_tags)
145+
ensure
146+
@notifier.unsubscribe block_sub
147+
end
122148
end
123149
end
124150
end

0 commit comments

Comments
 (0)