Skip to content

Commit

Permalink
Merge pull request #100 from saraid/guarantee-level_override-existence
Browse files Browse the repository at this point in the history
Guarantee level_override exists
  • Loading branch information
hsbt authored Aug 21, 2024
2 parents 436a7d6 + 3246f38 commit d5400dd
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 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[Fiber.current] || @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[Fiber.current] = level, Severity.coerce(severity)
begin
yield
ensure
if prev
@level_override[Fiber.current] = prev
level_override[Fiber.current] = prev
else
@level_override.delete(Fiber.current)
level_override.delete(Fiber.current)
end
end
end
Expand Down Expand Up @@ -746,6 +746,11 @@ def format_severity(severity)
SEV_LABEL[severity] || 'ANY'
end

# Guarantee the existence of this ivar even when subclasses don't call the superclass constructor.
def level_override
@level_override ||= {}
end

def format_message(severity, datetime, progname, msg)
(@formatter || @default_formatter).call(severity, datetime, progname, msg)
end
Expand Down

0 comments on commit d5400dd

Please sign in to comment.