Skip to content
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

Make the log level in DebugExceptions configurable #48575

Merged
merged 2 commits into from Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions actionpack/lib/action_dispatch/middleware/debug_exceptions.rb
Expand Up @@ -145,16 +145,18 @@ def log_error(request, wrapper)
message << " "
message.concat(trace)

log_array(logger, message)
log_array(logger, message, request)
end

def log_array(logger, lines)
def log_array(logger, lines, request)
return if lines.empty?

level = request.get_header("action_dispatch.debug_exception_log_level")

if logger.formatter && logger.formatter.respond_to?(:tags_text)
logger.fatal lines.join("\n#{logger.formatter.tags_text}")
logger.add(level, lines.join("\n#{logger.formatter.tags_text}"))
else
logger.fatal lines.join("\n")
logger.add(level, lines.join("\n"))
end
end

Expand Down
1 change: 1 addition & 0 deletions actionpack/lib/action_dispatch/railtie.rb
Expand Up @@ -26,6 +26,7 @@ class Railtie < Rails::Railtie # :nodoc:
config.action_dispatch.perform_deep_munge = true
config.action_dispatch.request_id_header = "X-Request-Id"
config.action_dispatch.log_rescued_responses = true
config.action_dispatch.debug_exception_log_level = :fatal

config.action_dispatch.default_headers = {
"X-Frame-Options" => "SAMEORIGIN",
Expand Down
13 changes: 13 additions & 0 deletions actionpack/test/dispatch/debug_exceptions_test.rb
Expand Up @@ -456,6 +456,19 @@ def call(env)
assert_match(/puke/, output.rewind && output.read)
end

test "logs at configured log level" do
@app = DevelopmentApp
output = StringIO.new
logger = Logger.new(output)
logger.level = Logger::WARN

get "/", headers: { "action_dispatch.show_exceptions" => :all, "action_dispatch.logger" => logger, "action_dispatch.debug_exception_log_level" => Logger::INFO }
assert_no_match(/puke/, output.rewind && output.read)

get "/", headers: { "action_dispatch.show_exceptions" => :all, "action_dispatch.logger" => logger, "action_dispatch.debug_exception_log_level" => Logger::ERROR }
assert_match(/puke/, output.rewind && output.read)
end

test "logs only what is necessary" do
@app = DevelopmentApp
io = StringIO.new
Expand Down
13 changes: 13 additions & 0 deletions guides/source/configuring.md
Expand Up @@ -61,6 +61,7 @@ Below are the default values associated with each target version. In cases of co
#### Default Values for Target Version 7.1

- [`config.action_controller.allow_deprecated_parameters_hash_equality`](#config-action-controller-allow-deprecated-parameters-hash-equality): `false`
- [`config.action_dispatch.debug_exception_log_level`](#config-action-dispatch-debug-exception-log-level): `:error`
- [`config.action_dispatch.default_headers`](#config-action-dispatch-default-headers): `{ "X-Frame-Options" => "SAMEORIGIN", "X-XSS-Protection" => "0", "X-Content-Type-Options" => "nosniff", "X-Permitted-Cross-Domain-Policies" => "none", "Referrer-Policy" => "strict-origin-when-cross-origin" }`
- [`config.action_view.sanitizer_vendor`](#config-action-view-sanitizer-vendor): `Rails::HTML::Sanitizer.best_supported_vendor`
- [`config.active_job.use_big_decimal_serializer`](#config-active-job-use-big-decimal-serializer): `true`
Expand Down Expand Up @@ -1689,6 +1690,18 @@ The default value depends on the `config.load_defaults` target version:
| (original) | `:marshal` |
| 7.0 | `:json` |

#### `config.action_dispatch.debug_exception_log_level`

Configure the log level used by the DebugExceptions middleware when logging
uncaught exceptions during requests

The default value depends on the `config.load_defaults` target version:

| Starting with version | The default value is |
| --------------------- | -------------------- |
| (original) | `:fatal` |
| 7.1 | `:error` |

#### `config.action_dispatch.default_headers`

Is a hash with HTTP headers that are set by default in each response.
Expand Down
8 changes: 8 additions & 0 deletions railties/CHANGELOG.md
@@ -1,3 +1,11 @@
* Add `config.action_dispatch.debug_exception_log_level` to configure the log
level used by `ActionDispatch::DebugExceptions`.

The default is `:fatal`, but with `load_defaults "7.1"` the default will be
`:error`.

*Hartley McGuire*

* Add `DATABASE` option to `railties:install:migrations`

This allows you to specify which database the migrations should be copied to
Expand Down
1 change: 1 addition & 0 deletions railties/lib/rails/application.rb
Expand Up @@ -303,6 +303,7 @@ def env_config
"action_dispatch.show_exceptions" => config.action_dispatch.show_exceptions,
"action_dispatch.show_detailed_exceptions" => config.consider_all_requests_local,
"action_dispatch.log_rescued_responses" => config.action_dispatch.log_rescued_responses,
"action_dispatch.debug_exception_log_level" => ActiveSupport::Logger.const_get(config.action_dispatch.debug_exception_log_level.to_s.upcase),
"action_dispatch.logger" => Rails.logger,
"action_dispatch.backtrace_cleaner" => Rails.backtrace_cleaner,
"action_dispatch.key_generator" => key_generator,
Expand Down
1 change: 1 addition & 0 deletions railties/lib/rails/application/configuration.rb
Expand Up @@ -297,6 +297,7 @@ def load_defaults(target_version)
"X-Permitted-Cross-Domain-Policies" => "none",
"Referrer-Policy" => "strict-origin-when-cross-origin"
}
action_dispatch.debug_exception_log_level = :error
end

if respond_to?(:active_job)
Expand Down
Expand Up @@ -190,3 +190,7 @@
# In previous versions of Rails, Action View always used `Rails::HTML4::Sanitizer`.
#
# Rails.application.config.action_view.sanitizer_vendor = Rails::HTML::Sanitizer.best_supported_vendor

# Configure the log level used by the DebugExceptions middleware when logging
# uncaught exceptions during requests
# Rails.application.config.action_dispatch.debug_exception_log_level = :error
30 changes: 30 additions & 0 deletions railties/test/application/configuration_test.rb
Expand Up @@ -2622,6 +2622,36 @@ def index
assert_equal :default, Rails.configuration.debug_exception_response_format
end

test "debug_exception_log_level is :fatal by default for upgraded apps" do
make_basic_app

class ::OmgController < ActionController::Base
def index
render plain: request.env["action_dispatch.debug_exception_log_level"]
end
end

get "/"

assert_equal "4", last_response.body
end

test "debug_exception_log_level is :error for new apps" do
make_basic_app do |app|
app.config.load_defaults "7.1"
end

class ::OmgController < ActionController::Base
def index
render plain: request.env["action_dispatch.debug_exception_log_level"]
end
end

get "/"

assert_equal "3", last_response.body
end

test "ActiveRecord::Base.has_many_inversing is true by default for new apps" do
app "development"

Expand Down