Class: Gallus::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/gallus/repository.rb

Overview

Internal: Wee need a place to keep track of registered loggers and their parents. This little repository class handles this task in a thread-safe manner.

Constant Summary collapse

PARENT_DELIMITER =
'::'

Class Method Summary collapse

Class Method Details

.allObject

Internal: Returns all registered loggers.



10
11
12
# File 'lib/gallus/repository.rb', line 10

def self.all
  @all ||= {}
end

.delete_with_children(name) ⇒ Object

Internal: For testing purposes we need to be able to delete logger alongside with all its children.



30
31
32
33
34
35
# File 'lib/gallus/repository.rb', line 30

def self.delete_with_children(name)
  @mutex.synchronize do
    all.keys.each { |k| all.delete(k) if k.start_with?(name + PARENT_DELIMITER) }
    all.delete(name)
  end
end

.find_parent(name) ⇒ Object

Internal: There must be a way to find parent logger for given class name. For example, looking up parent for Foo::Bar::Baz logger it’ll look up for Foo::Bar, then falling back to Foo and eventually to root logger if nothing found.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/gallus/repository.rb', line 17

def self.find_parent(name)
  parent, name = nil, name.dup

  while parent.nil?
    name = name.split(PARENT_DELIMITER)[0..-2].join(PARENT_DELIMITER)
    parent = all[name]
    break if name.empty?
  end

  parent
end

.get_or_create_logger(name, &block) ⇒ Object

Internal: We obviously need a way to create logger or retrieve it by name if already registered. Creation of logger causes configuration to be inherited from parent (or root) logger.



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gallus/repository.rb', line 39

def self.get_or_create_logger(name, &block)
  name, log = name.to_s, nil

  @mutex.synchronize do
    if log = all[name]
      yield log if block_given?
    else
      all[name] = (log = Class.new(Log).new(find_parent(name), name, &block))
    end
  end

  log
end