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

#deregister_log_child, #each_log_child, #logger=, #make_own_logger, #register_log_child, #reset_children_default_logger, #reset_default_logger, #reset_own_logger

Class Method Details

.extended(obj) ⇒ Object

:nodoc:


141
142
143
144
145
146
147
148
149
# File 'lib/utilrb/logger/hierarchy.rb', line 141

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:


135
136
137
138
139
# File 'lib/utilrb/logger/hierarchy.rb', line 135

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)

131
132
133
# File 'lib/utilrb/logger/hierarchy.rb', line 131

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


153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/utilrb/logger/hierarchy.rb', line 153

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 = parent_module
                parent_module.register_log_child(self)
            end
            parent_module.logger
        else
            self.class.logger
        end
end