Skip to content

Commit b98ee7d

Browse files
committed
Respect action_dispatch.log_rescued_responses
This is a new configuration option in Rails 7 which defaults to true. When an app sets this to false, this method does not log anything at all for rescued responses. Because rails_semantic_logger monkey patches ActionDispatch::DebugExceptions and overrides this method, this configuration option does not have any effect. Let's correct that. More context on this configuration option: - PR which introduced it rails/rails#42592 - Evidence that this implementation should work fine in Rails 7.1+ https://github.com/rails/rails/blob/v7.1.0/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb#L137 I also noticed that rails_semantic_logger is not respecting the action_dispatch.debug_exception_log_level configuration, and it makes its own decisions about what is an appropriate level. I updated this to also follow that configuration. This is also how it works in Rails 7.1+: https://github.com/rails/rails/blob/v7.1.0/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb#L154
1 parent c1ba442 commit b98ee7d

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

lib/rails_semantic_logger/extensions/action_dispatch/debug_exceptions.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ class DebugExceptions
77

88
undef_method :log_error
99
if (Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR >= 1) || Rails::VERSION::MAJOR > 7
10-
def log_error(_request, wrapper)
10+
def log_error(request, wrapper)
1111
Rails.application.deprecators.silence do
12-
level = wrapper.respond_to?(:rescue_response?) && wrapper.rescue_response? ? :debug : :fatal
12+
return if !log_rescued_responses?(request) && wrapper.rescue_response?
13+
14+
level = request.get_header("action_dispatch.debug_exception_log_level")
1315
ActionController::Base.logger.log(level, wrapper.exception)
1416
end
1517
end

test/controllers/articles_controller_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,26 @@ class ArticlesControllerTest < ActionDispatch::IntegrationTest
141141
assert_equal 4, messages.count, messages
142142
assert_kind_of ActiveRecord::RecordNotFound, messages[3].exception
143143
end
144+
145+
it "raises and does not log exception when action_dispatch.log_rescued_responses is false" do
146+
# we're testing ActionDispatch::DebugExceptions here too
147+
messages = semantic_logger_events do
148+
old_show = Rails.application.env_config["action_dispatch.show_exceptions"]
149+
old_log_rescued_responses = Rails.application.env_config["action_dispatch.log_rescued_responses"]
150+
151+
begin
152+
Rails.application.env_config["action_dispatch.show_exceptions"] = :all
153+
Rails.application.env_config["action_dispatch.log_rescued_responses"] = false
154+
get article_url(:show)
155+
rescue ActiveRecord::RecordNotFound => e
156+
# expected
157+
ensure
158+
Rails.application.env_config["action_dispatch.show_exceptions"] = old_show
159+
Rails.application.env_config["action_dispatch.log_rescued_responses"] = old_log_rescued_responses
160+
end
161+
end
162+
assert_equal 3, messages.count, messages
163+
end
144164
end
145165
end
146166
end

0 commit comments

Comments
 (0)