Module: Mixlib::Log
- Defined in:
- lib/mixlib/log.rb,
lib/mixlib/log/version.rb,
lib/mixlib/log/formatter.rb
Defined Under Namespace
Classes: Formatter
Constant Summary collapse
- LEVELS =
{ :debug => Logger::DEBUG, :info => Logger::INFO, :warn => Logger::WARN, :error => Logger::ERROR, :fatal => Logger::FATAL }.freeze
- LEVEL_NAMES =
LEVELS.invert.freeze
- VERSION =
"1.7.0"
Instance Method Summary collapse
- #<<(msg) ⇒ Object
- #add(severity, message = nil, progname = nil, &block) ⇒ Object (also: #log)
-
#init(*opts) ⇒ Object
Use Mixlib::Log.init when you want to set up the logger manually.
- #level(new_level = nil) ⇒ Object
-
#level=(new_level) ⇒ Object
Sets the level for the Logger object by symbol.
-
#logger ⇒ Object
init always returns a configured logger and creates a new one if it doesn’t yet exist.
-
#logger=(new_log_device) ⇒ Object
Sets the log device to
new_log_device. -
#loggers ⇒ Object
An Array of log devices that will be logged to.
-
#method_missing(method_symbol, *args, &block) ⇒ Object
Passes any other method calls on directly to the underlying Logger object created with init.
- #reset! ⇒ Object
- #use_log_devices(other) ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_symbol, *args, &block) ⇒ 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.
144 145 146 |
# File 'lib/mixlib/log.rb', line 144 def method_missing(method_symbol, *args, &block) loggers.each { |l| l.send(method_symbol, *args, &block) } end |
Instance Method Details
#<<(msg) ⇒ Object
131 132 133 |
# File 'lib/mixlib/log.rb', line 131 def <<(msg) loggers.each { |l| l << msg } end |
#add(severity, message = nil, progname = nil, &block) ⇒ Object Also known as: log
135 136 137 |
# File 'lib/mixlib/log.rb', line 135 def add(severity, = nil, progname = nil, &block) loggers.each { |l| l.add(severity, , progname, &block) } end |
#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 :warn level.
It also configures the Logger instance it creates to use the custom Mixlib::Log::Formatter class.
78 79 80 81 82 83 84 |
# File 'lib/mixlib/log.rb', line 78 def init(*opts) reset! @logger = logger_for(*opts) @logger.formatter = Mixlib::Log::Formatter.new() if @logger.respond_to?(:formatter=) @logger.level = Logger::WARN @logger end |
#level(new_level = nil) ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/mixlib/log.rb', line 101 def level(new_level = nil) if new_level.nil? LEVEL_NAMES[logger.level] else self.level = (new_level) end end |
#level=(new_level) ⇒ 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.
95 96 97 98 99 |
# File 'lib/mixlib/log.rb', line 95 def level=(new_level) level_int = LEVEL_NAMES.key?(new_level) ? new_level : LEVELS[new_level] raise ArgumentError, "Log level must be one of :debug, :info, :warn, :error, or :fatal" if level_int.nil? loggers.each { |l| l.level = level_int } end |
#logger ⇒ Object
init always returns a configured logger and creates a new one if it doesn’t yet exist
46 47 48 |
# File 'lib/mixlib/log.rb', line 46 def logger @logger || init end |
#logger=(new_log_device) ⇒ Object
Sets the log device to new_log_device. Any additional loggers that had been added to the loggers array will be cleared.
52 53 54 55 |
# File 'lib/mixlib/log.rb', line 52 def logger=(new_log_device) reset! @logger = new_log_device end |
#loggers ⇒ Object
An Array of log devices that will be logged to. Defaults to just the default
38 39 40 |
# File 'lib/mixlib/log.rb', line 38 def loggers @loggers ||= [logger] end |
#reset! ⇒ Object
31 32 33 34 |
# File 'lib/mixlib/log.rb', line 31 def reset! close! @logger, @loggers = nil, nil end |
#use_log_devices(other) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/mixlib/log.rb', line 57 def use_log_devices(other) if other.respond_to?(:loggers) && other.respond_to?(:logger) @loggers = other.loggers @logger = other.logger elsif other.kind_of?(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 end |