From ffc8475db88d7d210396a7afd8836d4296bf108b Mon Sep 17 00:00:00 2001 From: George Claghorn Date: Fri, 13 Sep 2019 13:54:18 -0400 Subject: [PATCH] Fix setting a more verbose thread-local log level We prepend a check against the thread-local level to Logger#add, but because it proceeds to check against the thread-global level, only setting a quieter thread-local level works. The quietest of the two wins. Fix by reimplementing #add entirely. It's unfortunate to have to do this, but I've already patched upstream Logger to prefer the level instance method over the @level instance variable, so we'll be able to avoid touching #add at all in the future. See https://github.com/ruby/logger/pull/41. --- .../logger_thread_safe_level.rb | 20 +++++++++++++++++-- activesupport/test/logger_test.rb | 13 ++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) 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)