Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable subclasses to configure level isolation #103

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lib/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ class Logger

# Logging severity threshold (e.g. <tt>Logger::INFO</tt>).
def level
level_override[Fiber.current] || @level
level_override[level_key] || @level
end

# Sets the log level; returns +severity+.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
42 changes: 39 additions & 3 deletions test/logger/test_severity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Comment on lines +79 to +80
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is mostly the same test as above, however, inside the Fiber the level does not get reset to INFO

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
Expand Down