Skip to content

Commit

Permalink
[ruby/logger] Enable subclasses to configure level isolation
Browse files Browse the repository at this point in the history
(ruby/logger#103)

`Logger#with_level` was recently added to enable configuring a
`Logger`'s level for the duration of a block. However, the configured
level is always tied to the currently running `Fiber`, which is not
always ideal in applications that mix `Thread`s and `Fiber`s.

For example, Active Support has provided a similar feature
(`ActiveSupport::Logger#log_at`) which, depending on configuration, can
be isolated to either `Thread`s or `Fiber`s.

This commit enables subclasses of `Logger` to customize the level
isolation. Ideally, it will enable replacing most of Active Support's
`#log_at`, since both methods end up serving the same purpose.

ruby/logger@dae2b832cd
  • Loading branch information
skipkayhil authored and matzbot committed Nov 7, 2024
1 parent 6bc6142 commit 342455e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
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)
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

0 comments on commit 342455e

Please sign in to comment.