Module: LogStashLogger::SilencedLogging

Defined in:
lib/logstash-logger/silenced_logging.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(logger) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/logstash-logger/silenced_logging.rb', line 21

def self.extended(logger)
  class << logger
    attr_accessor :silencer
    alias_method :level_without_thread_safety, :level
    alias_method :level, :level_with_thread_safety
    alias_method :add_without_thread_safety, :add
    alias_method :add, :add_with_thread_safety

    Logger::Severity.constants.each do |severity|
      instance_eval <<-EOT, __FILE__, __LINE__ + 1
        def #{severity.downcase}?                # def debug?
          Logger::#{severity} >= level           #   DEBUG >= level
        end                                      # end
      EOT
    end
  end

  logger.instance_eval do
    self.silencer = true
  end
end

Instance Method Details

#add_with_thread_safety(severity, message = nil, progname = nil, &block) ⇒ Object


55
56
57
58
59
60
61
# File 'lib/logstash-logger/silenced_logging.rb', line 55

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

#level_with_thread_safetyObject


51
52
53
# File 'lib/logstash-logger/silenced_logging.rb', line 51

def level_with_thread_safety
  thread_level || level_without_thread_safety
end

#silence(temporary_level = Logger::ERROR) ⇒ Object

Silences the logger for the duration of the block.


64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/logstash-logger/silenced_logging.rb', line 64

def silence(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

#thread_levelObject


43
44
45
# File 'lib/logstash-logger/silenced_logging.rb', line 43

def thread_level
  Thread.current[thread_hash_level_key]
end

#thread_level=(level) ⇒ Object


47
48
49
# File 'lib/logstash-logger/silenced_logging.rb', line 47

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