module Logger::Hierarchy

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.

Public Instance Methods

has_own_logger?() click to toggle source

Returns true if the local module has its own logger, and false if it returns the logger of the parent

# File lib/utilrb/logger/hierarchy.rb, line 110
def has_own_logger?
    defined?(@logger) && @logger
end
logger() click to toggle source

Returns the logger object that should be used to log at this level of the module hierarchy

Calls superclass method Logger::HierarchyElement#logger
# File lib/utilrb/logger/hierarchy.rb, line 132
def logger
    if logger = super
        return logger
    end

    @__utilrb_hierarchy__default_logger =
        if kind_of?(Module)
            m = self
            while m
                if m.name && !m.spacename.empty?
                    parent_module =
                        begin
                            constant(m.spacename)
                        rescue NameError
                        end
                    if parent_module.respond_to?(:logger)
                        break
                    end
                end

                if m.respond_to?(:superclass)
                    m = m.superclass
                    if m.respond_to?(:logger)
                        parent_module = m
                        break
                    end
                else
                    m = nil; break
                end
            end

            if !m
                raise NoParentLogger, "cannot find a logger for #{self}"
            end
            if parent_module.respond_to? :register_log_child
                parent_module.register_log_child(self)
            end
            parent_module.logger
        else
            self.class.logger
        end
end