Remove ResourceLogger and PlainLogger

This commit is contained in:
Mitchell Hashimoto 2011-07-10 15:35:45 -07:00
parent 050677b359
commit 0db2ec9d43
7 changed files with 11 additions and 188 deletions

View File

@ -283,7 +283,14 @@ module Vagrant
# @return [Logger]
def logger
return parent.logger if parent
@logger ||= Util::ResourceLogger.new(resource, self)
return @logger if defined?(@logger)
@logger = Logger.new(log_path.join("#{Time.now.to_i}.log"))
@logger.formatter = Proc.new do |severity, datetime, progname, msg|
"#{datetime} - [#{resource}] #{msg}"
end
@logger
end
# The root path is the path where the top-most (loaded last)

View File

@ -4,9 +4,7 @@ module Vagrant
autoload :Counter, 'vagrant/util/counter'
autoload :GlobLoader, 'vagrant/util/glob_loader'
autoload :HashWithIndifferentAccess, 'vagrant/util/hash_with_indifferent_access'
autoload :PlainLogger, 'vagrant/util/plain_logger'
autoload :Platform, 'vagrant/util/platform'
autoload :ResourceLogger, 'vagrant/util/resource_logger'
autoload :Retryable, 'vagrant/util/retryable'
autoload :SafeExec, 'vagrant/util/safe_exec'
autoload :StackedProcRunner, 'vagrant/util/stacked_proc_runner'

View File

@ -1,25 +0,0 @@
require 'logger'
module Vagrant
module Util
# Subclass of the standard library logger which has no format on
# its own. The message sent to the logger is outputted as-is.
class PlainLogger < ::Logger
# This is the method which is called for all debug, info, error,
# etc. methods by the logger. This is overriden to verify that
# the output is always flushed.
#
# Logger by default syncs all log devices but this just verifies
# it is truly flushed.
def add(*args)
super
@logdev.dev.flush if @logdev
end
def format_message(level, time, progname, msg)
# We do no formatting, its up to the user
"#{msg}\n"
end
end
end
end

View File

@ -1,56 +0,0 @@
module Vagrant
module Util
# Represents a logger for a specific resource within Vagrant. Each
# logger should be initialized and set to represent a single
# resource. Each logged message will then appear in the following
# format:
#
# [resource] message
#
class ResourceLogger
@@singleton_logger = nil
# The resource which this logger represents.
attr_reader :resource
# The environment that this logger is part of
attr_reader :env
# The backing logger which actually handles the IO. This logger
# should be a subclass of the standard library Logger, in general.
# IMPORTANT: This logger must be thread-safe.
attr_reader :logger
class << self
# Returns a singleton logger. If one has not yet be
# instantiated, then the given environment will be used to
# create a new logger.
def singleton_logger(env=nil)
return PlainLogger.new(nil) if !env.loaded?
@@singleton_logger ||= begin
file = env.log_path.join("#{Time.now.to_i}.log")
PlainLogger.new(file)
end
end
# Resets the singleton logger (only used for testing).
def reset_singleton_logger!
@@singleton_logger = nil
end
end
def initialize(resource, env)
@resource = resource
@env = env
@logger = self.class.singleton_logger(env)
end
[:debug, :info, :error, :fatal].each do |method|
define_method(method) do |message|
logger.send(method, "[#{resource}] #{message}")
end
end
end
end
end

View File

@ -256,12 +256,9 @@ class EnvironmentTest < Test::Unit::TestCase
context "loading logger" do
should "lazy load the logger only once" do
result = Vagrant::Util::ResourceLogger.new("vagrant", vagrant_env)
Vagrant::Util::ResourceLogger.expects(:new).returns(result).once
env = vagrant_env
assert_equal result, env.logger
assert_equal result, env.logger
assert_equal result, env.logger
result = env.logger
assert result === env.logger
end
should "return the parent's logger if a parent exists" do
@ -270,10 +267,7 @@ class EnvironmentTest < Test::Unit::TestCase
config.vm.define :db
vf
result = env.logger
Vagrant::Util::ResourceLogger.expects(:new).never
assert env.vms[:web].env.logger.equal?(result)
assert env.logger === env.vms[:web].env.logger
end
end

View File

@ -1,17 +0,0 @@
require "test_helper"
class PlainLoggerUtilTest < Test::Unit::TestCase
setup do
@klass = Vagrant::Util::PlainLogger
@instance = @klass.new(nil)
end
should "inherit from the standard logger" do
assert @instance.is_a?(::Logger)
end
should "just add a newline to the message" do
msg = "foo bar baz"
assert_equal "#{msg}\n", @instance.format_message("1", "2", "3", msg)
end
end

View File

@ -1,78 +0,0 @@
require "test_helper"
class ResourceLoggerUtilTest < Test::Unit::TestCase
setup do
@klass = Vagrant::Util::ResourceLogger
end
context "singleton logger" do
setup do
@klass.reset_singleton_logger!
@result = mock("result")
end
should "return a nil plain logger if the environment is not loaded" do
env = vagrant_env
env.stubs(:loaded?).returns(false)
Vagrant::Util::PlainLogger.expects(:new).with(nil).returns(@result)
assert_equal @result, @klass.singleton_logger(env)
end
should "return a logger with the output file set if environment is ready" do
env = vagrant_env
Vagrant::Util::PlainLogger.expects(:new).returns(@result).with() do |path|
assert path.to_s =~ /logs/
true
end
assert_equal @result, @klass.singleton_logger(env)
end
should "only load the logger once" do
env = vagrant_env
Vagrant::Util::PlainLogger.expects(:new).with(anything).returns(@result)
assert_equal @result, @klass.singleton_logger(env)
assert_equal @result, @klass.singleton_logger(env)
assert_equal @result, @klass.singleton_logger(env)
end
end
context "initialization" do
should "setup the logger and attributes" do
env = vagrant_env
resource = mock("resource")
result = mock("result")
@klass.expects(:singleton_logger).with(env).returns(result)
instance = @klass.new(resource, env)
assert_equal resource, instance.resource
assert_equal env, instance.env
assert_equal result, instance.logger
end
end
context "with an instance" do
setup do
@resource = "foo"
@env = vagrant_env
@logger = mock("logger")
@klass.stubs(:singleton_logger).returns(@logger)
@instance = @klass.new(@resource, @env)
end
context "logging methods" do
[:debug, :info, :error, :fatal].each do |method|
should "log with the proper format on #{method}" do
message = "bar"
@logger.expects(method).with("[#{@resource}] #{message}").once
@instance.send(method, message)
end
end
end
end
end