summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDaniel DeLeo <dan@opscode.com>2011-03-17 09:13:33 -0700
committerDaniel DeLeo <dan@opscode.com>2011-03-17 09:13:33 -0700
commite80574b33439ffbcd20b8401a0ce0fa6acb63cb9 (patch)
treec543adec8ddbf3f3513f80211a5de3b342167fc0 /lib
parentdc1c69935b1516e8f9bc7dd7c25dc234ba8e5740 (diff)
downloadmixlib-log-e80574b33439ffbcd20b8401a0ce0fa6acb63cb9.tar.gz
Add support for initializing Mixlib::Log with a logger
Diffstat (limited to 'lib')
-rw-r--r--lib/mixlib/log.rb28
1 files changed, 22 insertions, 6 deletions
diff --git a/lib/mixlib/log.rb b/lib/mixlib/log.rb
index 979c9d2..d0c99b5 100644
--- a/lib/mixlib/log.rb
+++ b/lib/mixlib/log.rb
@@ -22,7 +22,7 @@ require 'mixlib/log/formatter'
module Mixlib
module Log
- @logger = nil
+ @logger, @loggers = nil
LEVELS = { :debug=>Logger::DEBUG, :info=>Logger::INFO, :warn=>Logger::WARN, :error=>Logger::ERROR, :fatal=>Logger::FATAL}.freeze
LEVEL_NAMES = LEVELS.invert.freeze
@@ -46,8 +46,11 @@ module Mixlib
@logger || init
end
- def logger=(value)
- @logger=value
+ # Sets the log device to +new_log_device+. Any additional loggers
+ # that had been added to the +loggers+ array will be cleared.
+ def logger=(new_log_device)
+ reset!
+ @logger=new_log_device
end
def use_log_devices(other)
@@ -72,9 +75,9 @@ module Mixlib
#
# It also configures the Logger instance it creates to use the custom Mixlib::Log::Formatter class.
def init(*opts)
- @loggers = nil
- @logger = (opts.empty? ? Logger.new(STDOUT) : Logger.new(*opts))
- @logger.formatter = Mixlib::Log::Formatter.new()
+ reset!
+ @logger = logger_for(*opts)
+ @logger.formatter = Mixlib::Log::Formatter.new() if @logger.respond_to?(:formatter=)
@logger.level = Logger::WARN
@logger
end
@@ -132,6 +135,7 @@ module Mixlib
loggers.each {|l| l.add(severity, message = nil, progname = nil, &block) }
end
+ alias :log :add
# 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
@@ -140,5 +144,17 @@ module Mixlib
loggers.each {|l| l.send(method_symbol, *args, &block) }
end
+ private
+
+ def logger_for(*opts)
+ if opts.empty?
+ Logger.new(STDOUT)
+ elsif LEVELS.keys.inject(true) {|quacks, level| quacks && opts.first.respond_to?(level)}
+ opts.first
+ else
+ Logger.new(*opts)
+ end
+ end
+
end
end