diff options
-rw-r--r-- | lib/mixlib/log.rb | 10 | ||||
-rw-r--r-- | lib/mixlib/log/logger.rb | 2 | ||||
-rw-r--r-- | spec/mixlib/log_spec.rb | 19 |
3 files changed, 20 insertions, 11 deletions
diff --git a/lib/mixlib/log.rb b/lib/mixlib/log.rb index 491bbc8..e57ffcd 100644 --- a/lib/mixlib/log.rb +++ b/lib/mixlib/log.rb @@ -27,11 +27,12 @@ module Mixlib module Log include Logging - @logger, @loggers = nil def reset! + @logger ||= nil + @loggers ||= [] close! - @logger, @loggers = nil, nil + @logger = @loggers = nil @metadata = {} end @@ -46,7 +47,7 @@ module Mixlib # and creates a new one if it doesn't yet exist ## def logger - @logger || init + @logger ||= init end # Sets the log device to +new_log_device+. Any additional loggers @@ -170,7 +171,7 @@ module Mixlib def logger_for(*opts) if opts.empty? - Mixlib::Log::Logger.new(STDOUT) + Mixlib::Log::Logger.new($stdout) elsif LEVELS.keys.inject(true) { |quacks, level| quacks && opts.first.respond_to?(level) } opts.first else @@ -190,6 +191,7 @@ module Mixlib # via public API. In order to reduce amount of impact and # handle only File type log devices I had to use this method # to get access to it. + next unless logger.instance_variable_defined?(:"@logdev") next unless (logdev = logger.instance_variable_get(:"@logdev")) loggers_to_close << logger if logdev.filename end diff --git a/lib/mixlib/log/logger.rb b/lib/mixlib/log/logger.rb index f92d4a2..f227f23 100644 --- a/lib/mixlib/log/logger.rb +++ b/lib/mixlib/log/logger.rb @@ -21,7 +21,7 @@ module Mixlib # # +logdev+:: # The log device. This is a filename (String) or IO object (typically - # +STDOUT+, +STDERR+, or an open file). + # +$stdout+, +$stderr+, or an open file). # +shift_age+:: # Number of old log files to keep, *or* frequency of rotation (+daily+, # +weekly+ or +monthly+). diff --git a/spec/mixlib/log_spec.rb b/spec/mixlib/log_spec.rb index 5e0f270..b58c4e2 100644 --- a/spec/mixlib/log_spec.rb +++ b/spec/mixlib/log_spec.rb @@ -134,17 +134,22 @@ RSpec.describe Mixlib::Log do end it "should raise an ArgumentError if you try and set the level to something strange using the binding form" do - expect(lambda { Logit.level = :the_roots }).to raise_error(ArgumentError) + expect { Logit.level = :the_roots }.to raise_error(ArgumentError) end it "should raise an ArgumentError if you try and set the level to something strange using the method form" do - expect(lambda { Logit.level(:the_roots) }).to raise_error(ArgumentError) + expect { Logit.level(:the_roots) }.to raise_error(ArgumentError) end it "should pass other method calls directly to logger" do - Logit.level = :debug - expect(Logit).to be_debug - expect(lambda { Logit.debug("Gimme some sugar!") }).to_not raise_error + expect do + # this needs to be inside of the block because the level setting + # is causing the init, which grabs $stderr before rspec replaces + # it for output testing. + Logit.level = :debug + expect(Logit).to be_debug + Logit.debug("Gimme some sugar!") + end.to output(/DEBUG: Gimme some sugar!/).to_stdout end it "should pass add method calls directly to logger" do @@ -152,12 +157,13 @@ RSpec.describe Mixlib::Log do Logit.init(logdev) Logit.level = :debug expect(Logit).to be_debug - expect(lambda { Logit.add(Logger::DEBUG, "Gimme some sugar!") }).to_not raise_error + expect { Logit.add(Logger::DEBUG, "Gimme some sugar!") }.to_not raise_error expect(logdev.string).to match(/Gimme some sugar/) end it "should default to STDOUT if init is called with no arguments" do logger_mock = Struct.new(:formatter, :level).new + # intentionally STDOUT to avoid unfailable test expect(Logger).to receive(:new).with(STDOUT).and_return(logger_mock) Logit.init end @@ -203,6 +209,7 @@ RSpec.describe Mixlib::Log do end it "should return nil from its logging methods" do + # intentionally STDOUT to avoid unfailable test expect(Logger).to receive(:new).with(STDOUT) { double("a-quiet-logger").as_null_object } Logit.init |