Skip to content

Commit b6bb506

Browse files
committed
Fix config.log_level being ignored when using a Broadcast Logger:
- ### Problem If an application has configured a Broadcast Logger, setting the `config.log_level` to any value has no effect: ```ruby # config/application.rb config.logger = ActiveSupport::BroadcastLogger.new(Logger.new(STDOUT)) config.log_level = :warn puts Rails.logger.broadcasts.map(&:level) # => [Logger::DEBUG] ``` This is a side effect of rails#49621 which tried to fix the `log_level` default value overriding the whole broadcasts. ### Context The `log_level` default's value shouldn't apply to a Broadcast Logger, as otherwise it overrides whatever the application previously configured. While this behaviour is the same with a vanilla logger, at least we can workaround it: ```ruby # When using a vanilla logger config.logger = Logger.new(STDOUT, level: LOGGER::WARN) # Once the app boots, the level is overriden to DEBUG. We need to add the following line. config.log_level = :warn # When using a broadcast logger stdout = Logger.new(STDOUT, level: Logger::INFO) stderr = Logger.new(STDERR, level: Logger::ERROR) config.logger = ActiveSupport::BroadcastLogger(stdout, stderr) # Once the app boots the whole broadcast level is overriden to DEBUG. # There is no way to workaround this as you can't fine grain the level of each # loggers with `config.log_level=`. ``` ### Solution This PR ignores the default `log_level` value when using a Broadcast Logger, but ensure it gets used if an application manually sets it. Fix rails#50324
1 parent 89d8569 commit b6bb506

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

railties/lib/rails/application/bootstrap.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ module Bootstrap
5555
logger
5656
end
5757

58-
unless Rails.logger.is_a?(ActiveSupport::BroadcastLogger)
58+
if Rails.logger.is_a?(ActiveSupport::BroadcastLogger)
59+
if config.broadcast_log_level
60+
Rails.logger.level = ActiveSupport::Logger.const_get(config.broadcast_log_level.to_s.upcase)
61+
end
62+
else
5963
Rails.logger.level = ActiveSupport::Logger.const_get(config.log_level.to_s.upcase)
6064
broadcast_logger = ActiveSupport::BroadcastLogger.new(Rails.logger)
6165
broadcast_logger.formatter = Rails.logger.formatter

railties/lib/rails/application/configuration.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ class Configuration < ::Rails::Engine::Configuration
1919
:ssl_options, :public_file_server,
2020
:session_options, :time_zone, :reload_classes_only_on_change,
2121
:beginning_of_week, :filter_redirect, :x,
22-
:read_encrypted_secrets, :log_level, :content_security_policy_report_only,
22+
:read_encrypted_secrets, :content_security_policy_report_only,
2323
:content_security_policy_nonce_generator, :content_security_policy_nonce_directives,
2424
:require_master_key, :credentials, :disable_sandbox, :sandbox_by_default,
2525
:add_autoload_paths_to_load_path, :rake_eager_load, :server_timing, :log_file_size,
2626
:dom_testing_default_html_version
2727

28-
attr_reader :encoding, :api_only, :loaded_config_version
28+
attr_reader :encoding, :api_only, :loaded_config_version, :log_level
2929

3030
def initialize(*)
3131
super
@@ -373,6 +373,15 @@ def api_only=(value)
373373
@debug_exception_response_format ||= :api
374374
end
375375

376+
def log_level=(level)
377+
@log_level = level
378+
@broadcast_log_level = level
379+
end
380+
381+
def broadcast_log_level # :nodoc:
382+
defined?(@broadcast_log_level) ? @broadcast_log_level : nil
383+
end
384+
376385
def debug_exception_response_format
377386
@debug_exception_response_format || :default
378387
end

railties/test/application/configuration_test.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,6 +1949,31 @@ def index
19491949
assert_equal Logger::DEBUG, Rails.logger.level
19501950
end
19511951

1952+
test "config.log_level does not override the level of the broadcast with the default value" do
1953+
add_to_config <<-RUBY
1954+
stdout = Logger.new(STDOUT, level: Logger::INFO)
1955+
stderr = Logger.new(STDERR, level: Logger::ERROR)
1956+
config.logger = ActiveSupport::BroadcastLogger.new(stdout, stderr)
1957+
RUBY
1958+
1959+
app "development"
1960+
1961+
assert_equal([Logger::INFO, Logger::ERROR], Rails.logger.broadcasts.map(&:level))
1962+
end
1963+
1964+
test "config.log_level overrides the level of the broadcast when a custom value is set" do
1965+
add_to_config <<-RUBY
1966+
stdout = Logger.new(STDOUT)
1967+
stderr = Logger.new(STDERR)
1968+
config.logger = ActiveSupport::BroadcastLogger.new(stdout, stderr)
1969+
config.log_level = :warn
1970+
RUBY
1971+
1972+
app "development"
1973+
1974+
assert_equal([Logger::WARN, Logger::WARN], Rails.logger.broadcasts.map(&:level))
1975+
end
1976+
19521977
test "config.logger when logger is already a Broadcast Logger" do
19531978
logger = ActiveSupport::BroadcastLogger.new
19541979

0 commit comments

Comments
 (0)