Improve compatibility between Logger and ActiveSupport::BroadcastLogger

The usage of `dispatch` in all logging methods causes common usages such
as `logger.info` to return an array of loggers, making it unsafe for an
application to upgrade to Rails 7.1.

Returning `nil` is more efficient, and is the default behavior when
using `Logger`.
This commit is contained in:
Maximo Mussini 2024-05-29 11:34:04 -03:00 committed by Rafael Mendonça França
parent b9f814ad86
commit a054307bbc
No known key found for this signature in database
GPG Key ID: FC23B6D0F1EEE948
2 changed files with 6 additions and 0 deletions

@ -229,6 +229,7 @@ def initialize_copy(other)
private private
def dispatch(&block) def dispatch(&block)
@broadcasts.each { |logger| block.call(logger) } @broadcasts.each { |logger| block.call(logger) }
true
end end
def method_missing(name, ...) def method_missing(name, ...)

@ -302,6 +302,11 @@ def info(msg, &block)
assert_same logger, broadcast_logger.broadcasts.sole assert_same logger, broadcast_logger.broadcasts.sole
end end
test "logging has no return value" do
assert_equal true, @logger.info("Hello")
assert_equal true, @logger.error("Hello")
end
class CustomLogger class CustomLogger
attr_reader :adds, :closed, :chevrons attr_reader :adds, :closed, :chevrons
attr_accessor :level, :progname, :formatter, :local_level attr_accessor :level, :progname, :formatter, :local_level