Skip to content

Commit

Permalink
Resource logger now logs to a "logs" directory in the home path
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Sep 20, 2010
1 parent 7d89d01 commit e3ff9c7
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 14 deletions.
1 change: 0 additions & 1 deletion config/default.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Vagrant::Config.run do |config|
# default config goes here
config.vagrant.log_output = STDOUT
config.vagrant.dotfile_name = ".vagrant"
config.vagrant.home = "~/.vagrant"
config.vagrant.host = :detect
Expand Down
1 change: 0 additions & 1 deletion lib/vagrant/config/vagrant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ class VagrantConfig < Base
configures :vagrant

attr_accessor :dotfile_name
attr_accessor :log_output
attr_accessor :home
attr_accessor :host

Expand Down
7 changes: 6 additions & 1 deletion lib/vagrant/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Vagrant
# storing references to the various instances.
class Environment
ROOTFILE_NAME = "Vagrantfile"
HOME_SUBDIRS = ["tmp", "boxes"]
HOME_SUBDIRS = ["tmp", "boxes", "logs"]
DEFAULT_VM = :default

attr_reader :parent # Parent environment (in the case of multi-VMs)
Expand Down Expand Up @@ -73,6 +73,11 @@ def boxes_path
home_path.join("boxes")
end

# Path to the Vagrant logs directory
def log_path
home_path.join("logs")
end

# Returns the name of the resource which this environment represents.
# The resource is the VM name if there is a VM it represents, otherwise
# it defaults to "vagrant"
Expand Down
1 change: 0 additions & 1 deletion lib/vagrant/test_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def vagrantfile(*args)
str = args.shift || ""
File.open(path.to_s, "w") do |f|
f.puts "Vagrant::Config.run do |config|"
f.puts "config.vagrant.log_output = nil"
f.puts "config.vagrant.home = '#{home_path}'"
f.puts str
f.puts "end"
Expand Down
7 changes: 6 additions & 1 deletion lib/vagrant/util/resource_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ class << self
# instantiated, then the given environment will be used to
# create a new logger.
def singleton_logger(env=nil)
@@singleton_logger ||= PlainLogger.new(env.config.vagrant.log_output)
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).
Expand Down
18 changes: 9 additions & 9 deletions test/vagrant/util/resource_logger_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,29 @@ class ResourceLoggerUtilTest < Test::Unit::TestCase
@result = mock("result")
end

should "return a nil plain logger if the config is not loaded" do
should "return a nil plain logger if the environment is not loaded" do
env = vagrant_env
env.config.stubs(:loaded?).returns(false)
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 specified output if environment is ready" do
output = mock("output")
should "return a logger with the output file set if environment is ready" do
env = vagrant_env
env.config.vagrant.log_output = output

Vagrant::Util::PlainLogger.expects(:new).with(output).returns(@result)
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
output = mock("output")
env = vagrant_env
env.config.vagrant.log_output = output

Vagrant::Util::PlainLogger.expects(:new).with(output).returns(@result)
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)
Expand Down

0 comments on commit e3ff9c7

Please sign in to comment.