From 0db2ec9d43253630b000e3e919e29b1b867baa0d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 10 Jul 2011 15:35:45 -0700 Subject: [PATCH] Remove ResourceLogger and PlainLogger --- lib/vagrant/environment.rb | 9 ++- lib/vagrant/util.rb | 2 - lib/vagrant/util/plain_logger.rb | 25 -------- lib/vagrant/util/resource_logger.rb | 56 ---------------- test/vagrant/environment_test.rb | 12 +--- test/vagrant/util/plain_logger_test.rb | 17 ----- test/vagrant/util/resource_logger_test.rb | 78 ----------------------- 7 files changed, 11 insertions(+), 188 deletions(-) delete mode 100644 lib/vagrant/util/plain_logger.rb delete mode 100644 lib/vagrant/util/resource_logger.rb delete mode 100644 test/vagrant/util/plain_logger_test.rb delete mode 100644 test/vagrant/util/resource_logger_test.rb diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index 36036c271..f38e84b78 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -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) diff --git a/lib/vagrant/util.rb b/lib/vagrant/util.rb index 25d592105..78ccced0e 100644 --- a/lib/vagrant/util.rb +++ b/lib/vagrant/util.rb @@ -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' diff --git a/lib/vagrant/util/plain_logger.rb b/lib/vagrant/util/plain_logger.rb deleted file mode 100644 index 71927ee72..000000000 --- a/lib/vagrant/util/plain_logger.rb +++ /dev/null @@ -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 diff --git a/lib/vagrant/util/resource_logger.rb b/lib/vagrant/util/resource_logger.rb deleted file mode 100644 index f8f644125..000000000 --- a/lib/vagrant/util/resource_logger.rb +++ /dev/null @@ -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 diff --git a/test/vagrant/environment_test.rb b/test/vagrant/environment_test.rb index 5113f9c53..89d4c3337 100644 --- a/test/vagrant/environment_test.rb +++ b/test/vagrant/environment_test.rb @@ -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 diff --git a/test/vagrant/util/plain_logger_test.rb b/test/vagrant/util/plain_logger_test.rb deleted file mode 100644 index 7a3929c35..000000000 --- a/test/vagrant/util/plain_logger_test.rb +++ /dev/null @@ -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 diff --git a/test/vagrant/util/resource_logger_test.rb b/test/vagrant/util/resource_logger_test.rb deleted file mode 100644 index 7d4dcc3ea..000000000 --- a/test/vagrant/util/resource_logger_test.rb +++ /dev/null @@ -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