Module: Logger::Hierarchy

Includes:
HierarchyElement
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.

Defined Under Namespace

Classes: NoParentLogger

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HierarchyElement

#logger=, #make_own_logger, #reset_default_logger, #reset_own_logger

Class Method Details

.extended(obj) ⇒ Object

:nodoc:



103
104
105
106
107
108
109
110
111
# File 'lib/utilrb/logger/hierarchy.rb', line 103

def self.extended(obj) # :nodoc:
    obj.logger # initialize the default logger. Also does some checking
    if obj.kind_of?(Module) && !obj.spacename.empty?
        parent_module = constant(obj.spacename)
        if (parent_module.singleton_class.ancestors.include?(::Logger::Forward))
            obj.send(:extend, ::Logger::Forward)
        end
    end
end

.included(obj) ⇒ Object

:nodoc:



97
98
99
100
101
# File 'lib/utilrb/logger/hierarchy.rb', line 97

def self.included(obj) # :nodoc:
    if obj.singleton_class.ancestors.include?(::Logger::Forward)
        obj.send(:include, ::Logger::Forward)
    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

Returns:

  • (Boolean)


93
94
95
# File 'lib/utilrb/logger/hierarchy.rb', line 93

def has_own_logger?
    defined?(@logger) && @logger
end

#loggerObject

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



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/utilrb/logger/hierarchy.rb', line 115

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
                else
                    m = nil; break
                end
            end

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