Logger now uses singleton on the Vagrant::Logger class. Tests included.

This commit is contained in:
Mitchell Hashimoto 2010-02-10 22:12:34 -08:00
parent 39dd78c76f
commit e5903129e1
2 changed files with 56 additions and 13 deletions

View File

@ -12,16 +12,28 @@ error
end
def logger
# TODO: Buffer messages until config is loaded, then output them?
if Vagrant.config.loaded?
@logger ||= Vagrant::Logger.new(Vagrant.config.vagrant.log_output)
else
Vagrant::Logger.new(nil)
end
Logger.singleton_logger
end
end
class Logger < ::Logger
@@singleton_logger = nil
class <<self
def singleton_logger
# TODO: Buffer messages until config is loaded, then output them?
if Vagrant.config.loaded?
@@singleton_logger ||= Vagrant::Logger.new(Vagrant.config.vagrant.log_output)
else
Vagrant::Logger.new(nil)
end
end
def reset_logger!
@@singleton_logger = nil
end
end
def format_message(level, time, progname, msg)
"[#{level} #{time.strftime('%m-%d-%Y %X')}] Vagrant: #{msg}\n"
end

View File

@ -11,23 +11,54 @@ class UtilTest < Test::Unit::TestCase
end
context "logger" do
class OtherUtil
extend Vagrant::Util
end
setup do
@config = Vagrant::Config::Top.new
@config.stubs(:loaded?).returns(true)
@config.vagrant.log_output = STDOUT
Vagrant::Config.stubs(:config).returns(@config)
Vagrant::Logger.reset_logger!
end
teardown do
Vagrant::Logger.reset_logger!
end
should "return a logger to nil if config is not loaded" do
@config.expects(:loaded?).returns(false)
Vagrant::Logger.expects(:new).with(nil).once.returns("foo")
assert_equal "foo", RegUtil.logger
logger = RegUtil.logger
assert_nil logger.instance_variable_get(:@logdev)
end
should "return a logger using the configured output" do
@config.stubs(:loaded?).returns(true)
@config.vagrant.log_output = "foo"
Vagrant::Logger.expects(:new).once.with("foo").returns("bar")
assert_equal "bar", RegUtil.logger
assert_equal "bar", RegUtil.logger
logger = RegUtil.logger
logdev = logger.instance_variable_get(:@logdev)
assert logger
assert !logdev.nil?
assert_equal STDOUT, logdev.dev
end
should "only instantiate a logger once" do
Vagrant::Logger.expects(:new).once.returns("GOOD")
RegUtil.logger
RegUtil.logger
end
should "be able to reset the logger" do
Vagrant::Logger.expects(:new).twice
RegUtil.logger
Vagrant::Logger.reset_logger!
RegUtil.logger
end
should "return the same logger across classes" do
logger = RegUtil.logger
other = OtherUtil.logger
assert logger.equal?(other)
end
end
end