Sha256: 50d098b8547dc2115559413692e376677d93cbd5eb37bd2700c12291e1f88a9f

Contents?: true

Size: 964 Bytes

Versions: 1

Compression:

Stored size: 964 Bytes

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activesupport-5.0.0.beta2 lib/active_support/logger_silence.rb