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:
parent
42dea6b0f6
commit
ffc8475db8
|
@ -48,9 +48,25 @@ module ActiveSupport
|
||||||
local_level || super
|
local_level || super
|
||||||
end
|
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:
|
def add(severity, message = nil, progname = nil, &block) # :nodoc:
|
||||||
return true if @logdev.nil? || (severity || UNKNOWN) < level
|
severity ||= UNKNOWN
|
||||||
super
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -144,6 +144,19 @@ class LoggerTest < ActiveSupport::TestCase
|
||||||
assert_includes @output.string, "THIS IS HERE"
|
assert_includes @output.string, "THIS IS HERE"
|
||||||
end
|
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
|
def test_logger_silencing_works_for_broadcast
|
||||||
another_output = StringIO.new
|
another_output = StringIO.new
|
||||||
another_logger = ActiveSupport::Logger.new(another_output)
|
another_logger = ActiveSupport::Logger.new(another_output)
|
||||||
|
|
Loading…
Reference in New Issue