Sha256: d339fcb3fc9e439caf173f5e0bb96dca82f90b58dbdf14772eac06c2cf26760d

Contents?: true

Size: 1.89 KB

Versions: 2

Compression:

Stored size: 1.89 KB

Contents

# from https://github.com/rails/activerecord-session_store/blob/master/lib/active_record/session_store/extension/logger_silencer.rb
require "thread"
require "active_support/core_ext/class/attribute_accessors"
require "active_support/core_ext/module/aliasing"
require "active_support/core_ext/module/attribute_accessors"
require "active_support/concern"

module Ahoy
  module LoggerSilencer
    extend ActiveSupport::Concern

    included do
      cattr_accessor :silencer
      self.silencer = true
      alias_method_chain :level, :threadsafety
      alias_method_chain :add, :threadsafety
    end

    def thread_level
      Thread.current[thread_hash_level_key]
    end

    def thread_level=(level)
      Thread.current[thread_hash_level_key] = level
    end

    def level_with_threadsafety
      thread_level || level_without_threadsafety
    end

    def add_with_threadsafety(severity, message = nil, progname = nil, &block)
      if !defined?(@logdev) || @logdev.nil? || (severity || UNKNOWN) < level
        true
      else
        add_without_threadsafety(severity, message, progname, &block)
      end
    end

    # Silences the logger for the duration of the block.
    def silence_logger(temporary_level = Logger::ERROR)
      if silencer
        begin
          self.thread_level = temporary_level
          yield self
        ensure
          self.thread_level = nil
        end
      else
        yield self
      end
    end

    for severity in Logger::Severity.constants
      class_eval <<-EOT, __FILE__, __LINE__ + 1
        def #{severity.downcase}?                # def debug?
          Logger::#{severity} >= level           #   DEBUG >= level
        end                                      # end
      EOT
    end

    private

    def thread_hash_level_key
      @thread_hash_level_key ||= :"ThreadSafeLogger##{object_id}@level"
    end
  end
end

class NilLogger
  def self.silence_logger
    yield
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ahoy_matey-1.2.1 lib/ahoy/logger_silencer.rb
ahoy_matey-1.2.0 lib/ahoy/logger_silencer.rb