Class: ManageIQ::Loggers::Base

Inherits:
Logger
  • Object
show all
Defined in:
lib/manageiq/loggers/base.rb

Direct Known Subclasses

CloudWatch, JSONLogger, Journald

Defined Under Namespace

Classes: Formatter

Constant Summary collapse

MAX_LOG_LINE_LENGTH =
8.kilobytes

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*_, **_) ⇒ Base

Returns a new instance of Base.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/manageiq/loggers/base.rb', line 26

def initialize(*_, **_)
  super
  self.level = INFO

  # HACK: ActiveSupport monkey patches the standard Ruby Logger#initialize
  # method to set @formatter to a SimpleFormatter.
  #
  # The ActiveSupport Logger patches are deprecated in Rails 3.1.1 in favor of
  # ActiveSupport::BufferedLogger, so this hack may not be needed in future
  # version of Rails.
  self.formatter = Formatter.new

  # Allow for thread safe Logger level changes, similar to functionalities
  # provided by ActiveSupport::LoggerThreadSafeLevel
  @write_lock   = Mutex.new
  @local_levels = {}
end

Instance Attribute Details

#logdevObject

Expose logdev



60
61
62
# File 'lib/manageiq/loggers/base.rb', line 60

def logdev
  @logdev
end

Class Method Details

.log_hashes(logger, h, options = {}) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/manageiq/loggers/base.rb', line 105

def self.log_hashes(logger, h, options = {})
  require 'yaml'
  require 'manageiq/password'

  level  = options[:log_level] || :info
  filter = options[:filter] ? clean_log_hashes_filter(options[:filter]) : log_hashes_filter

  values = YAML.dump(h.to_hash).gsub(ManageIQ::Password::REGEXP, "[FILTERED]")
  values = values.split("\n").map do |l|
    if (key = filter.detect { |f| l.include?(f) })
      l.gsub!(/#{key}.*: (.+)/) { |m| m.gsub!($1, "[FILTERED]") }
    end
    l
  end.join("\n")

  logger.send(level, "\n#{values}")
end

.log_hashes_filterObject



97
98
99
# File 'lib/manageiq/loggers/base.rb', line 97

def self.log_hashes_filter
  @log_hashes_filter ||= ["password"]
end

.log_hashes_filter=(filters) ⇒ Object



101
102
103
# File 'lib/manageiq/loggers/base.rb', line 101

def self.log_hashes_filter=(filters)
  @log_hashes_filter = clean_log_hashes_filter(filters)
end

.wrap(source, *loggers) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/manageiq/loggers/base.rb', line 14

def wrap(source, *loggers)
  loggers.flatten!
  if ActiveSupport.gem_version >= Gem::Version.new("7.1.0")
    require 'active_support/broadcast_logger'
    ActiveSupport::BroadcastLogger.new(*loggers.unshift(source))
  else
    loggers.each { |logger| source.extend(ActiveSupport::Logger.broadcast(logger)) }
    source
  end
end

Instance Method Details

#levelObject



136
137
138
# File 'lib/manageiq/loggers/base.rb', line 136

def level
  local_level || super
end

#log_backtrace(err, level = :error) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/manageiq/loggers/base.rb', line 75

def log_backtrace(err, level = :error)
  # Get the name of the method that called us unless it is a wrapped log_backtrace
  method_name = nil
  caller.each do |c|
    method_name = c[/`([^']*)'/, 1]
    break unless method_name == 'log_backtrace'
  end

  # Log the error text
  send(level, "[#{err.class.name}]: #{err.message}  Method:[#{method_name}]")

  # Log the stack trace except for some specific exceptions
  unless (Object.const_defined?(:MiqException) && err.kind_of?(MiqException::Error)) ||
         (Object.const_defined?(:MiqAeException) && err.kind_of?(MiqAeException::Error))
    send(level, err.backtrace.nil? || err.backtrace.empty? ? "Backtrace is not available" : err.backtrace.join("\n"))
  end
end

#log_hashes(h, options = {}) ⇒ Object



131
132
133
134
# File 'lib/manageiq/loggers/base.rb', line 131

def log_hashes(h, options = {})
  options[:filter] ||= log_hashes_filter
  self.class.log_hashes(self, h, options)
end

#log_hashes_filterObject



123
124
125
# File 'lib/manageiq/loggers/base.rb', line 123

def log_hashes_filter
  @log_hashes_filter || self.class.log_hashes_filter
end

#log_hashes_filter=(filters) ⇒ Object



127
128
129
# File 'lib/manageiq/loggers/base.rb', line 127

def log_hashes_filter=(filters)
  @log_hashes_filter = self.class.send(:clean_log_hashes_filter, filters)
end

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

Silences the logger for the duration of the block.

Taken from activesupport/logger_silence



51
52
53
54
55
56
57
58
# File 'lib/manageiq/loggers/base.rb', line 51

def silence(temporary_level = Logger::ERROR)
  old_local_level  = local_level
  self.local_level = temporary_level

  yield self
ensure
  self.local_level = old_local_level
end

#wrap(loggers) ⇒ Object



44
45
46
# File 'lib/manageiq/loggers/base.rb', line 44

def wrap(loggers)
  self.class.wrap(self, loggers)
end