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.
This commit is contained in:
George Claghorn 2019-09-13 13:54:18 -04:00 committed by GitHub
parent 42dea6b0f6
commit ffc8475db8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 2 deletions

View File

@ -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

View File

@ -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)