Class: Vagrant::Util::ResourceLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/util/resource_logger.rb

Overview

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

This class is thread safe. The backing class which actually does all the logging IO is protected.

Constant Summary collapse

@@singleton_logger =
nil
@@writer_lock =
Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, env) ⇒ ResourceLogger

Returns a new instance of ResourceLogger.



48
49
50
51
52
# File 'lib/vagrant/util/resource_logger.rb', line 48

def initialize(resource, env)
  @resource = resource
  @env = env
  @logger = self.class.singleton_logger(env)
end

Instance Attribute Details

#envObject (readonly)

The environment that this logger is part of



22
23
24
# File 'lib/vagrant/util/resource_logger.rb', line 22

def env
  @env
end

#loggerObject (readonly)

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.



27
28
29
# File 'lib/vagrant/util/resource_logger.rb', line 27

def logger
  @logger
end

#resourceObject (readonly)

The resource which this logger represents.



19
20
21
# File 'lib/vagrant/util/resource_logger.rb', line 19

def resource
  @resource
end

Class Method Details

.reset_singleton_logger!Object

Resets the singleton logger (only used for testing).



43
44
45
# File 'lib/vagrant/util/resource_logger.rb', line 43

def reset_singleton_logger!
  @@singleton_logger = nil
end

.singleton_logger(env = nil) ⇒ Object

Returns a singleton logger. If one has not yet be instantiated, then the given environment will be used to create a new logger.



33
34
35
36
37
38
39
40
# File 'lib/vagrant/util/resource_logger.rb', line 33

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