Skip to content

Respect action_dispatch.log_rescued_responses #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

maxjacobson
Copy link

This PR is iterating a bit on #179

Description of changes

action_dispatch.log_rescued_responses 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:

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

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

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
Rails.application.deprecators.silence do
level = wrapper.respond_to?(:rescue_response?) && wrapper.rescue_response? ? :debug : :fatal
return if !log_rescued_responses?(request) && wrapper.rescue_response?
Copy link
Author

@maxjacobson maxjacobson Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In #179, there was a concern about the rescue_responses? method not working in Rails 5 and 6, which led to this respond_to? check being added. I don't believe it's necessary, however, because this is within a conditional that checks (Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR >= 1) || Rails::VERSION::MAJOR > 7, so we can feel confident that we are in Rails 7.1+. Given that, I took the liberty of removing the respond_to? check

@maxjacobson maxjacobson marked this pull request as ready for review July 10, 2025 20:39
level = wrapper.respond_to?(:rescue_response?) && wrapper.rescue_response? ? :debug : :fatal
return if !log_rescued_responses?(request) && wrapper.rescue_response?

level = request.get_header("action_dispatch.debug_exception_log_level")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making this more aligned with the default behavior of ActionDispatch::DebugExceptions makes sense to me. I don't think this is a breaking change, because the previous behavior was merged to the master branch, but never released. Let me know if you'd like me to remove this part though.

Copy link

@peggles2 peggles2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants