class Gallus::Repository

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.

Constants

PARENT_DELIMITER

Public Class Methods

all() click to toggle source

Internal: Returns all registered loggers.

# File lib/gallus/repository.rb, line 10
def self.all
  @all ||= {}
end
delete_with_children(name) click to toggle source

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

# 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) click to toggle source

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.

# 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) { |log| ... } click to toggle source

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.

# 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