summaryrefslogtreecommitdiff
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
parentdc1c69935b1516e8f9bc7dd7c25dc234ba8e5740 (diff)
downloadmixlib-log-e80574b33439ffbcd20b8401a0ce0fa6acb63cb9.tar.gz
Add support for initializing Mixlib::Log with a logger
-rw-r--r--lib/mixlib/log.rb28
-rw-r--r--spec/mixlib/log_spec.rb40
2 files changed, 58 insertions, 10 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
diff --git a/spec/mixlib/log_spec.rb b/spec/mixlib/log_spec.rb
index 1d1c4f7..00307f2 100644
--- a/spec/mixlib/log_spec.rb
+++ b/spec/mixlib/log_spec.rb
@@ -21,6 +21,22 @@ require 'tempfile'
require 'stringio'
require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
+class LoggerLike
+ attr_accessor :level
+ attr_reader :messages
+ def initialize
+ @messages = ""
+ end
+
+ [:debug, :info, :warn, :error, :fatal].each do |method_name|
+ class_eval(<<-E)
+ def #{method_name}(message)
+ @messages << message
+ end
+ E
+ end
+end
+
describe Mixlib::Log do
# Since we are testing class behaviour for an instance variable
@@ -29,13 +45,29 @@ describe Mixlib::Log do
Logit.reset!
end
- it "should accept regular options to Logger.new via init" do
- Tempfile.open("chef-test-log") do |tf|
- lambda { Logit.init(STDOUT) }.should_not raise_error
- lambda { Logit.init(tf) }.should_not raise_error
+ it "creates a logger using an IO object" do
+ io = StringIO.new
+ Logit.init(io)
+ Logit << "foo"
+ io.string.should match(/foo/)
+ end
+
+ it "creates a logger with a file name" do
+ Tempfile.open("chef-test-log") do |tempfile|
+ Logit.init(tempfile.path)
+ Logit << "bar"
+ tempfile.rewind
+ tempfile.read.should match(/bar/)
end
end
+ it "uses the logger provided when initialized with a logger like object" do
+ logger = LoggerLike.new
+ Logit.init(logger)
+ Logit.debug "qux"
+ logger.messages.should match(/qux/)
+ end
+
it "should re-initialize the logger if init is called again" do
first_logdev, second_logdev = StringIO.new, StringIO.new
Logit.init(first_logdev)