diff --git a/activesupport/lib/active_support/dependencies/zeitwerk_integration.rb b/activesupport/lib/active_support/dependencies/zeitwerk_integration.rb index 55fc92ee8b..23c237796e 100644 --- a/activesupport/lib/active_support/dependencies/zeitwerk_integration.rb +++ b/activesupport/lib/active_support/dependencies/zeitwerk_integration.rb @@ -26,6 +26,11 @@ module ActiveSupport cpath = object.is_a?(Module) ? object.name : object.to_s Rails.autoloaders.any? { |autoloader| autoloader.loaded?(cpath) } end + + def verbose=(verbose) + l = verbose ? (logger || Rails.logger).method(:debug) : nil + Rails.autoloaders.each { |autoloader| autoloader.logger = l } + end end class << self diff --git a/railties/test/application/zeitwerk_integration_test.rb b/railties/test/application/zeitwerk_integration_test.rb index 16ffbe56bb..c536c2f7f4 100644 --- a/railties/test/application/zeitwerk_integration_test.rb +++ b/railties/test/application/zeitwerk_integration_test.rb @@ -163,4 +163,40 @@ class ZeitwerkIntegrationTest < ActiveSupport::TestCase assert_equal %i(main_autoloader), $zeitwerk_integration_reload_test end + + test "verbose = true sets the debug method of the dependencies logger if present" do + boot + + logger = Logger.new(File::NULL) + ActiveSupport::Dependencies.logger = logger + ActiveSupport::Dependencies.verbose = true + + Rails.autoloaders.each do |autoloader| + assert_equal logger.method(:debug), autoloader.logger + end + end + + test "verbose = true sets the debug method of the Rails logger as fallback" do + boot + + ActiveSupport::Dependencies.verbose = true + + Rails.autoloaders.each do |autoloader| + assert_equal Rails.logger.method(:debug), autoloader.logger + end + end + + test "verbose = false sets loggers to nil" do + boot + + ActiveSupport::Dependencies.verbose = true + Rails.autoloaders.each do |autoloader| + assert autoloader.logger + end + + ActiveSupport::Dependencies.verbose = false + Rails.autoloaders.each do |autoloader| + assert_nil autoloader.logger + end + end end