class ManageIQ::Loggers::Base

Constants

MAX_LOG_LINE_LENGTH

Attributes

logdev[R]

Public Class Methods

log_hashes(logger, h, options = {}) click to toggle source
# File lib/manageiq/loggers/base.rb, line 75
def self.log_hashes(logger, h, options = {})
  require 'yaml'
  require 'manageiq/password'

  level  = options[:log_level] || :info
  filter = Array(options[:filter]).flatten.compact.map(&:to_s) << "password"
  filter.uniq!

  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
new(*args) click to toggle source
Calls superclass method
# File lib/manageiq/loggers/base.rb, line 12
def initialize(*args)
  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

Public Instance Methods

level() click to toggle source
Calls superclass method
# File lib/manageiq/loggers/base.rb, line 98
def level
  local_level || super
end
log_backtrace(err, level = :error) click to toggle source
# File lib/manageiq/loggers/base.rb, line 57
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 = {}) click to toggle source
# File lib/manageiq/loggers/base.rb, line 94
def log_hashes(h, options = {})
  self.class.log_hashes(self, h, options)
end
logdev=(logdev) click to toggle source
# File lib/manageiq/loggers/base.rb, line 44
def logdev=(logdev)
  if @logdev
    shift_age  = @logdev.instance_variable_get(:@shift_age)
    shift_size = @logdev.instance_variable_get(:@shift_size)
    @logdev.close
  else
    shift_age  = 0
    shift_size = 1048576
  end

  @logdev = LogDevice.new(logdev, :shift_age => shift_age, :shift_size => shift_size)
end
silence(temporary_level = Logger::ERROR) { |self| ... } click to toggle source

Silences the logger for the duration of the block.

Taken from activesupport/logger_silence

# File lib/manageiq/loggers/base.rb, line 33
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

Private Instance Methods

local_level() click to toggle source
# File lib/manageiq/loggers/base.rb, line 108
def local_level
  @local_levels[local_log_id]
end
local_level=(level) click to toggle source
# File lib/manageiq/loggers/base.rb, line 112
def local_level=(level)
  @write_lock.synchronize do
    if level
      @local_levels[local_log_id] = level
    else
      @local_levels.delete(local_log_id)
    end
  end
end
local_log_id() click to toggle source
# File lib/manageiq/loggers/base.rb, line 104
def local_log_id
  Thread.current.object_id
end