564e427c05
The Logger severity predicates have existed since the [introduction of Logger][1]. However, these methods only looked at the `level` instance variable, so they did not work with the [thread safe implementation][2] of temporary log levels in Rails. Since then, the Logger severity predicates were [updated][3] to use the `level` method instead of the instance variable, making Rails' severity predicate overrides obsolete. This commit removes Rails' custom severity predicates in favor of Logger's implementation, since the new implementation was released in Logger 1.4.2 and came bundled with Ruby 2.7.0. [1]: ruby/logger@525b58d97e [2]: rails/rails@629efb6057 [3]: ruby/logger@7365c995bf
48 lines
1.0 KiB
Ruby
48 lines
1.0 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "active_support/concern"
|
|
require "logger"
|
|
|
|
module ActiveSupport
|
|
module LoggerThreadSafeLevel # :nodoc:
|
|
extend ActiveSupport::Concern
|
|
|
|
def local_level
|
|
IsolatedExecutionState[local_level_key]
|
|
end
|
|
|
|
def local_level=(level)
|
|
case level
|
|
when Integer
|
|
when Symbol
|
|
level = Logger::Severity.const_get(level.to_s.upcase)
|
|
when nil
|
|
else
|
|
raise ArgumentError, "Invalid log level: #{level.inspect}"
|
|
end
|
|
if level.nil?
|
|
IsolatedExecutionState.delete(local_level_key)
|
|
else
|
|
IsolatedExecutionState[local_level_key] = level
|
|
end
|
|
end
|
|
|
|
def level
|
|
local_level || super
|
|
end
|
|
|
|
# Change the thread-local level for the duration of the given block.
|
|
def log_at(level)
|
|
old_local_level, self.local_level = local_level, level
|
|
yield
|
|
ensure
|
|
self.local_level = old_local_level
|
|
end
|
|
|
|
private
|
|
def local_level_key
|
|
@local_level_key ||= :"logger_thread_safe_level_#{object_id}"
|
|
end
|
|
end
|
|
end
|