Module: Logger::Hierarchy
- Defined in:
- lib/utilrb/logger/hierarchy.rb
Overview
Define a hierarchy of loggers mapped to the module hierarchy.
It defines the #logger accessor which either returns the logger attribute of the module, if one is defined, or its parent logger attribute.
This module is usually used in conjunction with the Logger::Root method:
module First
extend Logger.Root("First", :INFO)
module Second
extend Hierarchy
end
end
Second.logger will return First.logger. If we do Second.make_own_logger, then a different object will be returned.
“extend Hierarchy” will also add the Forward support if the parent module has it.
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the logger object that should be used to log at this level of the module hierarchy.
Class Method Summary collapse
-
.extended(obj) ⇒ Object
:nodoc:.
-
.included(obj) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#has_own_logger? ⇒ Boolean
Returns true if the local module has its own logger, and false if it returns the logger of the parent.
-
#make_own_logger(new_progname = nil, new_level = nil) ⇒ Object
Makes it so that this level of the module hierarchy has its own logger.
-
#reset_own_logger ⇒ Object
Removes a logger defined at this level of the module hierarchy.
Instance Attribute Details
#logger ⇒ Object
Returns the logger object that should be used to log at this level of the module hierarchy
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/utilrb/logger/hierarchy.rb', line 80 def logger if defined?(@logger) && @logger return @logger elsif defined?(@default_logger) && @default_logger return @default_logger end @default_logger ||= if kind_of?(Module) constant(self.spacename).logger else self.class.logger end end |
Class Method Details
.extended(obj) ⇒ Object
:nodoc:
41 42 43 44 45 46 47 48 |
# File 'lib/utilrb/logger/hierarchy.rb', line 41 def self.extended(obj) # :nodoc: if obj.kind_of?(Module) parent_module = constant(obj.spacename) if (parent_module.singleton_class.ancestors.include?(Logger::Forward)) obj.send(:extend, Logger::Forward) end end end |
Instance Method Details
#has_own_logger? ⇒ Boolean
Returns true if the local module has its own logger, and false if it returns the logger of the parent
52 53 54 |
# File 'lib/utilrb/logger/hierarchy.rb', line 52 def has_own_logger? defined?(@logger) && @logger end |
#make_own_logger(new_progname = nil, new_level = nil) ⇒ Object
Makes it so that this level of the module hierarchy has its own logger. If new_progname
and/or new_level
are nil, the associated value are taken from the parent’s logger.
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/utilrb/logger/hierarchy.rb', line 59 def make_own_logger(new_progname = nil, new_level = nil) if !has_own_logger? @logger = self.logger.dup end if new_progname @logger.progname = new_progname end if new_level @logger.level = new_level end @logger end |
#reset_own_logger ⇒ Object
Removes a logger defined at this level of the module hierarchy. The logging methods will now access the parent’s module logger.
74 75 76 |
# File 'lib/utilrb/logger/hierarchy.rb', line 74 def reset_own_logger @logger = nil end |