lib/logging/repository.rb in TwP-logging-1.0.0 vs lib/logging/repository.rb in TwP-logging-1.1.0
- old
+ new
@@ -16,10 +16,11 @@
#
# This is a singleton class -- use the +instance+ method to obtain the
# +Repository+ instance.
#
def initialize
+ @masters = []
@h = {:root => ::Logging::RootLogger.new}
# configures the internal logger which is disabled by default
logger = ::Logging::Logger.allocate
logger._setup(
@@ -105,11 +106,11 @@
#
# If you have a class A::B::C, then the parent of C is B, and the parent
# of B is A. Parents are determined by namespace.
#
def parent( key )
- name = _parent_name(to_key(key))
+ name = parent_name(to_key(key))
return if name.nil?
@h[name]
end
# call-seq:
@@ -124,11 +125,11 @@
ary = []
parent = to_key(parent)
@h.each_pair do |child,logger|
next if :root == child
- ary << logger if parent == _parent_name(child)
+ ary << logger if parent == parent_name(child)
end
return ary.sort
end
# call-seq:
@@ -152,19 +153,56 @@
end
# Returns the name of the parent for the logger identified by the given
# _key_. If the _key_ is for the root logger, then +nil+ is returned.
#
- def _parent_name( key )
+ def parent_name( key )
return if :root == key
a = key.split PATH_DELIMITER
p = :root
while a.slice!(-1) and !a.empty?
k = a.join PATH_DELIMITER
if @h.has_key? k then p = k; break end
end
p
+ end
+
+ # call-seq:
+ # add_master( 'First::Name', 'Second::Name', ... )
+ #
+ # Add the given logger names to the list of consolidation masters. All
+ # classes in the given namespace(s) will use these loggers instead of
+ # creating their own individual loggers.
+ #
+ def add_master( *args )
+ args.map do |key|
+ key = to_key(key)
+ @masters << key unless @masters.include? key
+ key
+ end
+ end
+
+ # call-seq:
+ # master_for( key )
+ #
+ # Retruns the consolidation master name for the given _key_. If there is
+ # no consolidation master, then +nil+ is returned.
+ #
+ def master_for( key )
+ return if @masters.empty?
+ key = to_key(key)
+
+ loop do
+ break key if @masters.include? key
+ break nil if :root == key
+
+ if index = key.rindex(PATH_DELIMITER)
+ key = key.slice(0, index)
+ else
+ key = :root
+ end
+ end
end
end # class Repository
end # module Logging