Module: Mixlib::Log

Defined in:
lib/mixlib/log.rb,
lib/mixlib/log/formatter.rb

Defined Under Namespace

Classes: Formatter

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol, *args) ⇒ Object

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.



69
70
71
# File 'lib/mixlib/log.rb', line 69

def method_missing(method_symbol, *args)
  logger.send(method_symbol, *args)
end

Instance Attribute Details

#loggerObject

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



32
33
34
# File 'lib/mixlib/log.rb', line 32

def logger
  init
end

Instance Method Details

#init(*opts) ⇒ Object

Use Mixlib::Log.init when you want to set up the logger manually. Arguments to this method get passed directly to 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 :info level.

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



43
44
45
46
47
48
49
# File 'lib/mixlib/log.rb', line 43

def init(*opts)
  if @logger.nil?
    @logger = (opts.empty? ? Logger.new(STDOUT) : Logger.new(*opts))
    @logger.formatter = Mixlib::Log::Formatter.new()
  end
  @logger
end

#level(loglevel = :warn) ⇒ Object

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

:debug
:info
:warn
:error
:fatal

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

Raises:

  • (ArgumentError)


60
61
62
63
64
# File 'lib/mixlib/log.rb', line 60

def level(loglevel=:warn)
  level = { :debug=>Logger::DEBUG, :info=>Logger::INFO, :warn=>Logger::WARN, :error=>Logger::ERROR, :fatal=>Logger::FATAL}[loglevel]
  raise ArgumentError, "Log level must be one of :debug, :info, :warn, :error, or :fatal" if level.nil?
  logger.level = level
end