diff --git a/activesupport/lib/active_support/logger_thread_safe_level.rb b/activesupport/lib/active_support/logger_thread_safe_level.rb index 1775a414926..3735f0395b4 100644 --- a/activesupport/lib/active_support/logger_thread_safe_level.rb +++ b/activesupport/lib/active_support/logger_thread_safe_level.rb @@ -48,9 +48,25 @@ module ActiveSupport local_level || super end + # Redefined to check severity against #level, and thus the thread-local level, rather than +@level+. + # FIXME: Remove when the minimum Ruby version supports overriding Logger#level. def add(severity, message = nil, progname = nil, &block) # :nodoc: - return true if @logdev.nil? || (severity || UNKNOWN) < level - super + severity ||= UNKNOWN + progname ||= @progname + + return true if @logdev.nil? || severity < level + + if message.nil? + if block_given? + message = yield + else + message = progname + progname = @progname + end + end + + @logdev.write \ + format_message(format_severity(severity), Time.now, progname, message) end end end diff --git a/activesupport/test/logger_test.rb b/activesupport/test/logger_test.rb index 6f7a1860223..c7a89f676d7 100644 --- a/activesupport/test/logger_test.rb +++ b/activesupport/test/logger_test.rb @@ -144,6 +144,19 @@ class LoggerTest < ActiveSupport::TestCase assert_includes @output.string, "THIS IS HERE" end + def test_unsilencing + @logger.level = Logger::INFO + + @logger.debug "NOT THERE" + + @logger.silence Logger::DEBUG do + @logger.debug "THIS IS HERE" + end + + assert_not @output.string.include?("NOT THERE") + assert_includes @output.string, "THIS IS HERE" + end + def test_logger_silencing_works_for_broadcast another_output = StringIO.new another_logger = ActiveSupport::Logger.new(another_output)