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

Delegate block in broadcast logger method_missing #49458

Merged
merged 1 commit into from
Oct 3, 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
12 changes: 8 additions & 4 deletions activesupport/lib/active_support/broadcast_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,20 @@ def dispatch(&block)
@broadcasts.each { |logger| block.call(logger) }
end

def method_missing(name, *args)
def method_missing(name, *args, &block)
loggers = @broadcasts.select { |logger| logger.respond_to?(name) }

if loggers.none?
super(name, *args)
super(name, *args, &block)
elsif loggers.one?
loggers.first.send(name, *args)
loggers.first.send(name, *args, &block)
else
loggers.map { |logger| logger.send(name, *args) }
loggers.map { |logger| logger.send(name, *args, &block) }
end
end

def respond_to_missing?(method, include_all)
@broadcasts.any? { |logger| logger.respond_to?(method, include_all) }
end
end
end
14 changes: 14 additions & 0 deletions activesupport/test/broadcast_logger_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,16 @@ def info(msg, &block)
assert(logger.foo)
end

test "calling a method that accepts a block" do
logger = BroadcastLogger.new(CustomLogger.new)

called = false
logger.bar do
called = true
end
assert(called)
end

class CustomLogger
attr_reader :adds, :closed, :chevrons
attr_accessor :level, :progname, :formatter, :local_level
Expand All @@ -286,6 +296,10 @@ def foo
true
end

def bar
yield
end

def debug(message, &block)
add(::Logger::DEBUG, message, &block)
end
Expand Down