Object
The Repository is a hash that stores references to all Loggers that have been created. It provides methods to determine parent/child relationships between Loggers and to retrieve Loggers from the hash.
nodoc:
This is a singleton class — use the instance method to obtain the Repository instance.
# File lib/logging/repository.rb, line 20 20: def initialize 21: @masters = [] 22: @h = {:root => ::Logging::RootLogger.new} 23: 24: # configures the internal logger which is disabled by default 25: logger = ::Logging::Logger.allocate 26: logger._setup( 27: to_key(::Logging), 28: :parent => @h[:root], 29: :additive => false, 30: :level => ::Logging::LEVELS.length # turns this logger off 31: ) 32: @h[logger.name] = logger 33: end
Returns the Logger named name.
When name is a String or a Symbol it will be used “as is” to retrieve the logger. When name is a Class the class name will be used to retrieve the logger. When name is an object the name of the object’s class will be used to retrieve the logger.
Example:
repo = Repository.instance obj = MyClass.new log1 = repo[obj] log2 = repo[MyClass] log3 = repo['MyClass'] log1.object_id == log2.object_id # => true log2.object_id == log3.object_id # => true
# File lib/logging/repository.rb, line 57 57: def []( key ) @h[to_key(key)] end
Stores the logger under the given name.
When name is a String or a Symbol it will be used “as is” to store the logger. When name is a Class the class name will be used to store the logger. When name is an object the name of the object’s class will be used to store the logger.
# File lib/logging/repository.rb, line 69 69: def []=( key, val ) @h[to_key(key)] = val end
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.
# File lib/logging/repository.rb, line 177 177: def add_master( *args ) 178: args.map do |key| 179: key = to_key(key) 180: @masters << key unless @masters.include? key 181: key 182: end 183: end
Returns an array of the children loggers for the logger identified by key where key follows the same identification rules described in +Repository#[]+. Children are returned regardless of the existence of the logger referenced by key.
# File lib/logging/repository.rb, line 124 124: def children( parent ) 125: ary = [] 126: parent = to_key(parent) 127: 128: @h.each_pair do |child,logger| 129: next if :root == child 130: ary << logger if parent == parent_name(child) 131: end 132: return ary.sort 133: end
Returns the Logger named name. An IndexError will be raised if the logger does not exist.
When name is a String or a Symbol it will be used “as is” to retrieve the logger. When name is a Class the class name will be used to retrieve the logger. When name is an object the name of the object’s class will be used to retrieve the logger.
# File lib/logging/repository.rb, line 82 82: def fetch( key ) @h.fetch(to_key(key)) end
Returns true if the given logger exists in the repository. Returns false if this is not the case.
When name is a String or a Symbol it will be used “as is” to retrieve the logger. When name is a Class the class name will be used to retrieve the logger. When name is an object the name of the object’s class will be used to retrieve the logger.
# File lib/logging/repository.rb, line 95 95: def has_logger?( key ) @h.has_key?(to_key(key)) end
Returns the consolidation master name for the given key. If there is no consolidation master, then nil is returned.
# File lib/logging/repository.rb, line 191 191: def master_for( key ) 192: return if @masters.empty? 193: key = to_key(key) 194: 195: loop do 196: break key if @masters.include? key 197: break nil if :root == key 198: 199: if index = key.rindex(PATH_DELIMITER) 200: key = key.slice(0, index) 201: else 202: key = :root 203: end 204: end 205: end
Returns the parent logger for the logger identified by key where key follows the same identification rules described in Repository#[]. A parent is returned regardless of the existence of the logger referenced by key.
A note about parents -
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.
# File lib/logging/repository.rb, line 110 110: def parent( key ) 111: name = parent_name(to_key(key)) 112: return if name.nil? 113: @h[name] 114: 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.
# File lib/logging/repository.rb, line 158 158: def parent_name( key ) 159: return if :root == key 160: 161: a = key.split PATH_DELIMITER 162: p = :root 163: while a.slice!(1) and !a.empty? 164: k = a.join PATH_DELIMITER 165: if @h.has_key? k then p = k; break end 166: end 167: p 168: end
Takes the given key and converts it into a form that can be used to retrieve a logger from the Repository hash.
When key is a String or a Symbol it will be returned “as is”. When key is a Class the class name will be returned. When key is an object the name of the object’s class will be returned.
# File lib/logging/repository.rb, line 145 145: def to_key( key ) 146: case key 147: when :root, 'root'; :root 148: when String; key 149: when Symbol; key.to_s 150: when Module; key.logger_name 151: when Object; key.class.logger_name 152: end 153: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.