summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel DeLeo <dan@opscode.com>2010-10-13 21:59:40 -0700
committerDaniel DeLeo <dan@opscode.com>2010-10-13 21:59:40 -0700
commit8885078f68aac2f14c8cd185d2032dc583ec4821 (patch)
treec035d77d7ce3fa7c5fc49334f11c8b7c97d8ee0e
parent42bfc83c42a384e1371b9d3bd4b3a48bbae3e9cf (diff)
parentf9fb2da9a9511c97a3552c153f77c3ff7c0754b5 (diff)
downloadmixlib-log-8885078f68aac2f14c8cd185d2032dc583ec4821.tar.gz
Merge branch 'CHEF-1382'
-rw-r--r--lib/mixlib/log.rb14
-rw-r--r--spec/mixlib/log_spec.rb29
2 files changed, 30 insertions, 13 deletions
diff --git a/lib/mixlib/log.rb b/lib/mixlib/log.rb
index e71eeb9..025018d 100644
--- a/lib/mixlib/log.rb
+++ b/lib/mixlib/log.rb
@@ -30,7 +30,7 @@ module Mixlib
# and creates a new one if it doesn't yet exist
##
def logger
- init
+ @logger || init
end
def logger=(value)
@@ -45,11 +45,9 @@ module Mixlib
#
# It also configures the Logger instance it creates to use the custom Mixlib::Log::Formatter class.
def init(*opts)
- if @logger.nil?
- @logger = (opts.empty? ? Logger.new(STDOUT) : Logger.new(*opts))
- @logger.formatter = Mixlib::Log::Formatter.new()
- @logger.level = Logger::WARN
- end
+ @logger = (opts.empty? ? Logger.new(STDOUT) : Logger.new(*opts))
+ @logger.formatter = Mixlib::Log::Formatter.new()
+ @logger.level = Logger::WARN
@logger
end
@@ -79,8 +77,8 @@ module Mixlib
# 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.
- def method_missing(method_symbol, *args)
- logger.send(method_symbol, *args)
+ def method_missing(method_symbol, *args, &block)
+ logger.send(method_symbol, *args, &block)
end
end
diff --git a/spec/mixlib/log_spec.rb b/spec/mixlib/log_spec.rb
index f07ff66..d064871 100644
--- a/spec/mixlib/log_spec.rb
+++ b/spec/mixlib/log_spec.rb
@@ -29,10 +29,21 @@ describe Mixlib::Log do
end
it "should accept regular options to Logger.new via init" do
- tf = Tempfile.new("chef-test-log")
- tf.open
- lambda { Logit.init(STDOUT) }.should_not raise_error
- lambda { Logit.init(tf) }.should_not raise_error
+ Tempfile.open("chef-test-log") do |tf|
+ lambda { Logit.init(STDOUT) }.should_not raise_error
+ lambda { Logit.init(tf) }.should_not raise_error
+ end
+ 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)
+ Logit.fatal "FIRST"
+ first_logdev.string.should match(/FIRST/)
+ Logit.init(second_logdev)
+ Logit.fatal "SECOND"
+ first_logdev.string.should_not match(/SECOND/)
+ second_logdev.string.should match(/SECOND/)
end
it "should set the log level using the binding form, with :debug, :info, :warn, :error, or :fatal" do
@@ -49,7 +60,15 @@ describe Mixlib::Log do
Logit.level.should == symbol
end
end
-
+
+ it "passes blocks to the underlying logger object" do
+ logdev = StringIO.new
+ Logit.init(logdev)
+ Logit.fatal { "the_message" }
+ logdev.string.should match(/the_message/)
+ end
+
+
it "should set the log level using the method form, with :debug, :info, :warn, :error, or :fatal" do
levels = {
:debug => Logger::DEBUG,