module Mixlib::Log

Constants

VERSION

Attributes

metadata[RW]

Public Instance Methods

<<(msg) click to toggle source
# File lib/mixlib/log.rb, line 135
def <<(msg)
  loggers.each { |l| l << msg }
end
add(severity, message = nil, progname = nil, data: {}) { || ... } click to toggle source
# File lib/mixlib/log.rb, line 139
def add(severity, message = nil, progname = nil, data: {}, &block)
  message, progname, data = yield if block_given?
  data = metadata.merge(data) if metadata.is_a?(Hash) && data.is_a?(Hash)
  loggers.each do |l|
    # if we don't have any metadata, let's not do the potentially expensive
    # merging and managing that this call requires
    if l.respond_to?(:add_data) && !data.nil? && !data.empty?
      l.add_data(severity, message, progname, data: data)
    else
      l.add(severity, message, progname)
    end
  end
end
Also aliased as: log
configured?() click to toggle source

Let the application query if logging objects have been set up

# File lib/mixlib/log.rb, line 94
def configured?
  @configured
end
init(*opts) click to toggle source

Use #init when you want to set up the logger manually. Arguments to this method get passed directly to Mixlib::Log::Logger.new, so check out the documentation for the standard Logger class to understand what to do here.

If this method is called with no arguments, it will log to STDOUT at the :warn level.

It also configures the Logger instance it creates to use the custom Mixlib::Log::Formatter class.

# File lib/mixlib/log.rb, line 82
def init(*opts)
  reset!
  @logger = logger_for(*opts)
  @logger.formatter = Mixlib::Log::Formatter.new if @logger.respond_to?(:formatter=)
  @logger.level = Logger::WARN
  @configured = true
  @parent = nil
  @metadata = {}
  @logger
end
level(new_level = nil) click to toggle source
# File lib/mixlib/log.rb, line 117
def level(new_level = nil)
  if new_level.nil?
    LEVEL_NAMES[logger.level]
  else
    self.level = (new_level)
  end
end
level=(new_level) click to toggle source

Sets the level for the Logger object by symbol. Valid arguments are:

:trace
:debug
:info
:warn
:error
:fatal

Throws an ArgumentError if you feed it a bogus log level.

# File lib/mixlib/log.rb, line 110
def level=(new_level)
  level_int = LEVEL_NAMES.key?(new_level) ? new_level : LEVELS[new_level]
  raise ArgumentError, "Log level must be one of :trace, :debug, :info, :warn, :error, or :fatal" if level_int.nil?

  loggers.each { |l| l.level = level_int }
end
log(severity, message = nil, progname = nil, data: {}, &block)
Alias for: add
logger() click to toggle source

init always returns a configured logger and creates a new one if it doesn't yet exist

# File lib/mixlib/log.rb, line 49
def logger
  @logger ||= init
end
logger=(new_log_device) click to toggle source

Sets the log device to new_log_device. Any additional loggers that had been added to the loggers array will be cleared.

# File lib/mixlib/log.rb, line 55
def logger=(new_log_device)
  reset!
  @logger = new_log_device
end
loggers() click to toggle source

An Array of log devices that will be logged to. Defaults to just the default logger log device, but you can push to this array to add more devices.

# File lib/mixlib/log.rb, line 41
def loggers
  @loggers ||= [logger]
end
method_missing(method_symbol, *args, &block) click to toggle source

Passes any other method calls on directly to the underlying Logger object created with init. If this method gets hit before a call to Mixlib::Logger.init has been made, it will call Mixlib::Logger.init() with no arguments.

# File lib/mixlib/log.rb, line 167
def method_missing(method_symbol, *args, &block)
  loggers.each { |l| l.send(method_symbol, *args, &block) }
end
reset!() click to toggle source
# File lib/mixlib/log.rb, line 31
def reset!
  @logger  ||= nil
  @loggers ||= []
  close!
  @logger = @loggers = nil
  @metadata = {}
end
use_log_devices(other) click to toggle source
# File lib/mixlib/log.rb, line 60
def use_log_devices(other)
  if other.respond_to?(:loggers) && other.respond_to?(:logger)
    @loggers = other.loggers
    @logger = other.logger
  elsif other.is_a?(Array)
    @loggers = other
    @logger = other.first
  else
    msg = "#use_log_devices takes a Mixlib::Log object or array of log devices. " <<
      "You gave: #{other.inspect}"
    raise ArgumentError, msg
  end
  @configured = true
end
with_child(metadata = {}) { |child| ... } click to toggle source
# File lib/mixlib/log.rb, line 155
def with_child(metadata = {})
  child = Child.new(self, metadata)
  if block_given?
    yield child
  else
    child
  end
end

Private Instance Methods

all_loggers() click to toggle source
# File lib/mixlib/log.rb, line 183
def all_loggers
  [@logger, *@loggers].uniq
end
close!() click to toggle source
# File lib/mixlib/log.rb, line 203
def close!
  # try to close all file loggers
  loggers_to_close.each do |l|
    l.close rescue nil
  end
end
logger_for(*opts) click to toggle source
# File lib/mixlib/log.rb, line 173
def logger_for(*opts)
  if opts.empty?
    Mixlib::Log::Logger.new($stdout)
  elsif LEVELS.keys.inject(true) { |quacks, level| quacks && opts.first.respond_to?(level) }
    opts.first
  else
    Mixlib::Log::Logger.new(*opts)
  end
end
loggers_to_close() click to toggle source

select all loggers with File log devices

# File lib/mixlib/log.rb, line 188
def loggers_to_close
  loggers_to_close = []
  all_loggers.each do |logger|
    # unfortunately Logger does not provide access to the logdev
    # via public API. In order to reduce amount of impact and
    # handle only File type log devices I had to use this method
    # to get access to it.
    next unless logger.instance_variable_defined?(:"@logdev")
    next unless (logdev = logger.instance_variable_get(:"@logdev"))

    loggers_to_close << logger if logdev.filename
  end
  loggers_to_close
end