Class: Log4r::Logger::Repository

Inherits:
Object
  • Object
show all
Extended by:
MonitorMixin
Includes:
Singleton
Defined in:
lib/log4r-color/repository.rb

Overview

The repository stores a Hash of loggers keyed to their fullnames and provides a few functions to reduce the code bloat in log4r-color/logger.rb. This class is supposed to be transparent to end users, hence it is a class within Logger. If anyone knows how to make this private, let me know.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRepository

Returns a new instance of Repository.



31
32
33
# File 'lib/log4r-color/repository.rb', line 31

def initialize
	@loggers = Hash.new
end

Instance Attribute Details

#loggersObject (readonly)

Returns the value of attribute loggers.



29
30
31
# File 'lib/log4r-color/repository.rb', line 29

def loggers
  @loggers
end

Class Method Details

.[](fullname) ⇒ Object



35
36
37
38
39
# File 'lib/log4r-color/repository.rb', line 35

def self.[](fullname)
	self.synchronize do
	  instance.loggers[fullname]
	end # exclusive
end

.[]=(fullname, logger) ⇒ Object



41
42
43
44
45
# File 'lib/log4r-color/repository.rb', line 41

def self.[]=(fullname, logger)
	self.synchronize do
	  instance.loggers[fullname] = logger
	end # exclusive
end

.all_children(parent) ⇒ Object

Retrieves all children of a parent



48
49
50
51
52
53
54
55
56
# File 'lib/log4r-color/repository.rb', line 48

def self.all_children(parent)
	# children have the parent name + delimiter in their fullname
	daddy = parent.name + Private::Config::LoggerPathDelimiter
	self.synchronize do
	  for fullname, logger in instance.loggers
	    yield logger if parent.is_root? || fullname =~ /#{daddy}/
	  end
	end # exclusive
end

.find_ancestor(path) ⇒ Object

looks for the first defined logger in a child’s path or nil if none found (which will then be rootlogger)



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/log4r-color/repository.rb', line 72

def self.find_ancestor(path)
	arr = path.split Log4rConfig::LoggerPathDelimiter
	logger = nil
	self.synchronize do
	  while arr.size > 0 do
	    logger = Repository[arr.join(Log4rConfig::LoggerPathDelimiter)]
	    break unless logger.nil?
	    arr.pop
	  end
	end # exclusive
	logger
end

.reassign_any_children(parent) ⇒ Object

when new loggers are introduced, they may get inserted into an existing inheritance tree. this method updates the children of a logger to link their new parent



61
62
63
64
65
66
67
68
# File 'lib/log4r-color/repository.rb', line 61

def self.reassign_any_children(parent)
	self.synchronize do
	  for fullname, logger in instance.loggers
	    next if logger.is_root?
	    logger.parent = parent if logger.path =~ /^#{parent.fullname}$/
	  end
	end # exclusive
end