Class: Logging::Repository
- Inherits:
-
Object
- Object
- Logging::Repository
- Includes:
- Singleton
- Defined in:
- lib/logging/repository.rb
Overview
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.
Constant Summary collapse
- PATH_DELIMITER =
:nodoc:
'::'
Instance Method Summary collapse
-
#[](key) ⇒ Object
call-seq: instance.
-
#[]=(key, val) ⇒ Object
call-seq: instance = logger.
-
#children(key) ⇒ Object
call-seq: children( key ).
-
#fetch(key) ⇒ Object
call-seq: fetch( name ).
-
#has_logger?(key) ⇒ Boolean
call-seq: has_logger?( name ).
-
#initialize ⇒ Repository
constructor
nodoc:.
-
#parent(key) ⇒ Object
call-seq: parent( key ).
-
#to_key(key) ⇒ Object
call-seq: to_key( key ).
Constructor Details
#initialize ⇒ Repository
nodoc:
This is a singleton class – use the instance
method to obtain the Repository
instance.
21 22 23 |
# File 'lib/logging/repository.rb', line 21 def initialize @h = {:root => ::Logging::RootLogger.new} end |
Instance Method Details
#[](key) ⇒ Object
call-seq:
instance[name]
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
47 |
# File 'lib/logging/repository.rb', line 47 def []( key ) @h[to_key(key)] end |
#[]=(key, val) ⇒ Object
call-seq:
instance[name] = logger
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.
59 |
# File 'lib/logging/repository.rb', line 59 def []=( key, val ) @h[to_key(key)] = val end |
#children(key) ⇒ Object
call-seq:
children( key )
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.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/logging/repository.rb', line 115 def children( key ) key = to_key(key) depth = key.split(PATH_DELIMITER).length rgxp = Regexp.new "^#{key}#{PATH_DELIMITER}" a = @h.keys.map do |k| if k =~ rgxp l = @h[k] d = l.parent.name.split(PATH_DELIMITER).length if d <= depth then l else nil end end end a.compact.sort end |
#fetch(key) ⇒ Object
call-seq:
fetch( name )
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.
72 |
# File 'lib/logging/repository.rb', line 72 def fetch( key ) @h.fetch(to_key(key)) end |
#has_logger?(key) ⇒ Boolean
call-seq:
has_logger?( name )
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.
85 |
# File 'lib/logging/repository.rb', line 85 def has_logger?( key ) @h.has_key?(to_key(key)) end |
#parent(key) ⇒ Object
call-seq:
parent( key )
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.
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/logging/repository.rb', line 95 def parent( key ) key = to_key(key) a = key.split PATH_DELIMITER p = @h[:root] while a.slice!(-1) and !a.empty? k = a.join PATH_DELIMITER if @h.has_key? k then p = @h[k]; break end end p end |
#to_key(key) ⇒ Object
call-seq:
to_key( key )
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.
140 141 142 143 144 145 146 |
# File 'lib/logging/repository.rb', line 140 def to_key( key ) case key when Symbol, String; key when Class; key.name when Object; key.class.name end end |