mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
629efb6057
- uses instance defined level if no custom local log level defined - Keeps track of local log level per [ thread + object-instance ] - prevents memory leakage by removing local level hash key/value on #silence method exit - avoids the use of Thread local variables
45 lines
965 B
Ruby
45 lines
965 B
Ruby
require 'active_support/concern'
|
|
require 'active_support/core_ext/module/attribute_accessors'
|
|
require 'concurrent'
|
|
|
|
module LoggerSilence
|
|
extend ActiveSupport::Concern
|
|
|
|
included do
|
|
cattr_accessor :silencer
|
|
attr_reader :local_levels
|
|
self.silencer = true
|
|
end
|
|
|
|
def after_initialize
|
|
@local_levels = Concurrent::Map.new(:initial_capacity => 2)
|
|
end
|
|
|
|
def local_log_id
|
|
Thread.current.__id__
|
|
end
|
|
|
|
def level
|
|
local_levels[local_log_id] || super
|
|
end
|
|
|
|
# Silences the logger for the duration of the block.
|
|
def silence(temporary_level = Logger::ERROR)
|
|
if silencer
|
|
begin
|
|
old_local_level = local_levels[local_log_id]
|
|
local_levels[local_log_id] = temporary_level
|
|
|
|
yield self
|
|
ensure
|
|
if old_local_level
|
|
local_levels[local_log_id] = old_local_level
|
|
else
|
|
local_levels.delete(local_log_id)
|
|
end
|
|
end
|
|
else
|
|
yield self
|
|
end
|
|
end
|
|
end
|