Module: Logger::HierarchyElement

Included in:
Hierarchy
Defined in:
lib/utilrb/logger/hierarchy.rb

Instance Method Summary collapse

Instance Method Details

#deregister_log_child(child) ⇒ Object


31
32
33
34
35
36
37
38
39
# File 'lib/utilrb/logger/hierarchy.rb', line 31

def deregister_log_child(child)
    log_children.delete_if do |ref|
        begin
            ref.__getobj__ == child
        rescue WeakRef::RefError
            true
        end
    end
end

#each_log_childObject


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/utilrb/logger/hierarchy.rb', line 41

def each_log_child
    return enum_for(__method__) if !block_given?

    log_children.delete_if do |ref|
        begin
            yield(ref.__getobj__)
            false
        rescue WeakRef::RefError
            true
        end
    end
end

#loggerObject


92
93
94
95
96
97
98
# File 'lib/utilrb/logger/hierarchy.rb', line 92

def logger
    if defined?(@logger) && @logger
        return @logger
    elsif defined?(@__utilrb_hierarchy__default_logger) && @__utilrb_hierarchy__default_logger
        return @__utilrb_hierarchy__default_logger
    end
end

#logger=(new_logger) ⇒ Object

Allows to change the logger object at this level of the hierarchy

This is usually not used directly: a new logger can be created with Hierarchy#make_own_logger and removed with Hierarchy#reset_own_logger


75
76
77
78
# File 'lib/utilrb/logger/hierarchy.rb', line 75

def logger=(new_logger)
    @logger = new_logger
    reset_children_default_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.


16
17
18
19
20
21
22
23
24
25
# File 'lib/utilrb/logger/hierarchy.rb', line 16

def make_own_logger(new_progname = nil, new_level = nil)
    new_logger = @logger || self.logger.dup
    if new_progname
        new_logger.progname = new_progname
    end
    if new_level
        new_logger.level = new_level
    end
    self.logger = new_logger
end

#register_log_child(child) ⇒ Object


27
28
29
# File 'lib/utilrb/logger/hierarchy.rb', line 27

def register_log_child(child)
    log_children << WeakRef.new(child)
end

#reset_children_default_loggerObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resets the default logger of this context's children

This is called whenever the context children is reset, since the cached default logger is now invalid


60
61
62
63
64
65
66
67
68
69
# File 'lib/utilrb/logger/hierarchy.rb', line 60

def reset_children_default_logger
    children = log_children
    @log_children = Array.new
    children.each do |ref|
        begin
            ref.__getobj__.reset_default_logger
        rescue WeakRef::RefError
        end
    end
end

#reset_default_loggerObject


86
87
88
89
90
# File 'lib/utilrb/logger/hierarchy.rb', line 86

def reset_default_logger
    @__utilrb_hierarchy__default_logger = nil
    @parent_module.deregister_log_child(self) if @parent_module
    reset_children_default_logger
end

#reset_own_loggerObject

Removes a logger defined at this level of the module hierarchy. The logging methods will now access the parent's module logger.


82
83
84
# File 'lib/utilrb/logger/hierarchy.rb', line 82

def reset_own_logger
    self.logger = nil
end