diff --git a/lib/logger.rb b/lib/logger.rb index 4735209..8d6e0d7 100644 --- a/lib/logger.rb +++ b/lib/logger.rb @@ -381,7 +381,7 @@ class Logger # Logging severity threshold (e.g. Logger::INFO). def level - level_override[Fiber.current] || @level + level_override[level_key] || @level end # Sets the log level; returns +severity+. @@ -406,14 +406,14 @@ def level=(severity) # logger.debug { "Hello" } # end def with_level(severity) - prev, level_override[Fiber.current] = level, Severity.coerce(severity) + prev, level_override[level_key] = level, Severity.coerce(severity) begin yield ensure if prev - level_override[Fiber.current] = prev + level_override[level_key] = prev else - level_override.delete(Fiber.current) + level_override.delete(level_key) end end end @@ -751,6 +751,10 @@ def level_override @level_override ||= {} end + def level_key + Fiber.current + end + def format_message(severity, datetime, progname, msg) (@formatter || @default_formatter).call(severity, datetime, progname, msg) end diff --git a/test/logger/test_severity.rb b/test/logger/test_severity.rb index e1069c8..fb26939 100644 --- a/test/logger/test_severity.rb +++ b/test/logger/test_severity.rb @@ -26,7 +26,7 @@ def test_level_assignment end end - def test_thread_local_level + def test_fiber_local_level logger = Logger.new(nil) logger.level = INFO # default level other = Logger.new(nil) @@ -40,14 +40,50 @@ def test_thread_local_level logger.with_level(DEBUG) do # verify reentrancy assert_equal(logger.level, DEBUG) - Thread.new do + Fiber.new do assert_equal(logger.level, INFO) logger.with_level(:WARN) do assert_equal(other.level, ERROR) assert_equal(logger.level, WARN) end assert_equal(logger.level, INFO) - end.join + end.resume + + assert_equal(logger.level, DEBUG) + end + assert_equal(logger.level, WARN) + end + assert_equal(logger.level, INFO) + end + + def test_thread_local_level + subclass = Class.new(Logger) do + def level_key + Thread.current + end + end + + logger = subclass.new(nil) + logger.level = INFO # default level + other = subclass.new(nil) + other.level = ERROR # default level + + assert_equal(other.level, ERROR) + logger.with_level(:WARN) do + assert_equal(other.level, ERROR) + assert_equal(logger.level, WARN) + + logger.with_level(DEBUG) do # verify reentrancy + assert_equal(logger.level, DEBUG) + + Fiber.new do + assert_equal(logger.level, DEBUG) + logger.with_level(:WARN) do + assert_equal(other.level, ERROR) + assert_equal(logger.level, WARN) + end + assert_equal(logger.level, DEBUG) + end.resume assert_equal(logger.level, DEBUG) end