Module: Mixlib::Log
- Includes:
- Logging
- Defined in:
- lib/mixlib/log.rb,
lib/mixlib/log/child.rb,
lib/mixlib/log/logger.rb,
lib/mixlib/log/logging.rb,
lib/mixlib/log/version.rb,
lib/mixlib/log/formatter.rb
Defined Under Namespace
Modules: Logging Classes: Child, Formatter, Logger
Constant Summary collapse
- VERSION =
"3.1.1".freeze
Constants included from Logging
Logging::LEVELS, Logging::LEVEL_NAMES, Logging::SEV_LABEL, Logging::TRACE
Instance Attribute Summary collapse
-
#metadata ⇒ Object
Returns the value of attribute metadata.
Instance Method Summary collapse
- #<<(msg) ⇒ Object
- #add(severity, message = nil, progname = nil, data: {}, &block) ⇒ Object (also: #log)
-
#configured? ⇒ Boolean
Let the application query if logging objects have been set up.
-
#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
- #with_child(metadata = {}) ⇒ Object
Methods included from Logging
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.
167 168 169 |
# File 'lib/mixlib/log.rb', line 167 def method_missing(method_symbol, *args, &block) loggers.each { |l| l.send(method_symbol, *args, &block) } end |
Instance Attribute Details
#metadata ⇒ Object
Returns the value of attribute metadata.
98 99 100 |
# File 'lib/mixlib/log.rb', line 98 def @metadata end |
Instance Method Details
#<<(msg) ⇒ Object
135 136 137 |
# File 'lib/mixlib/log.rb', line 135 def <<(msg) loggers.each { |l| l << msg } end |
#add(severity, message = nil, progname = nil, data: {}, &block) ⇒ Object Also known as: log
139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/mixlib/log.rb', line 139 def add(severity, = nil, progname = nil, data: {}, &block) , progname, data = yield if block_given? data = .merge(data) if .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, , progname, data: data) else l.add(severity, , progname) end end end |
#configured? ⇒ Boolean
Let the application query if logging objects have been set up
94 95 96 |
# File 'lib/mixlib/log.rb', line 94 def configured? @configured 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.
82 83 84 85 86 87 88 89 90 91 |
# 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) ⇒ Object
117 118 119 120 121 122 123 |
# 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) ⇒ Object
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.
110 111 112 113 114 115 |
# 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 |
#logger ⇒ Object
init always returns a configured logger and creates a new one if it doesn’t yet exist
49 50 51 |
# File 'lib/mixlib/log.rb', line 49 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.
55 56 57 58 |
# File 'lib/mixlib/log.rb', line 55 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 @logger log device, but you can push to this array to add more devices.
41 42 43 |
# File 'lib/mixlib/log.rb', line 41 def loggers @loggers ||= [logger] end |
#reset! ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/mixlib/log.rb', line 31 def reset! @logger ||= nil @loggers ||= [] close! @logger = @loggers = nil @metadata = {} end |
#use_log_devices(other) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# 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 |