Class: Logging::Repository

### Home Classes Methods

### In Files

- lib/logging/repository.rb

### Parent

Object

### Namespace

- MODULE Logging::Repository::Singleton

### Methods

- ::new
- #[]
- #[]=
- #add_master
- #children
- #fetch
- #has_logger?
- #master_for
- #parent
- #parent_name
- #to_key

### Included Modules

- Logging::Repository::Singleton

### Files

- History.txt
- README.rdoc

### Class/Module Index

Quicksearch

- Logging
- Logging::Appender
- Logging::Appenders
- Logging::Appenders::Buffering
- Logging::Appenders::Email
- Logging::Appenders::File
- Logging::Appenders::Growl
- Logging::Appenders::IO
- Logging::Appenders::RollingFile
- Logging::Appenders::Stderr
- Logging::Appenders::Stdout
- Logging::Appenders::StringIo
- Logging::Appenders::Syslog
- Logging::ColorScheme
- Logging::Config
- Logging::Config::Configurator
- Logging::Config::Configurator::DSL
- Logging::Config::Configurator::Error
- Logging::Config::Configurator::TopLevelDSL
- Logging::Config::YamlConfigurator
- Logging::Config::YamlConfigurator::Error
- Logging::Layout
- Logging::Layouts
- Logging::Layouts::Basic
- Logging::Layouts::Parseable
- Logging::Layouts::Pattern
- Logging::Logger
- Logging::MappedDiagnosticContext
- Logging::NestedDiagnosticContext
- Logging::Proxy
- Logging::RailsCompat
- Logging::Repository
- Logging::Repository::Singleton
- Logging::RootLogger
- Logging::Stats
- Logging::Stats::Sampler
- Logging::Stats::Tracker
- RSpec
- RSpec::Core
- RSpec::Core::Configuration
- RSpec::LoggingHelper
- RSpec::LoggingHelper
- Spec
- Spec::Runner
- Spec::Runner::Configuration
- Object
- Object::LoggingHelper
- File
- FileUtils
- Hash
- Kernel
- Module
- ReentrantMutex
- String

No matching classes.

# Logging::Repository

The Repository is a hash that stores references to all Loggers that have been created. It provides methods to determine parent/child relationships between Loggers and to retrieve Loggers from the hash.

### Public Class Methods

new() click to toggle source

nodoc:

This is a singleton class -- use the `instance` method to obtain the `Repository` instance.

    # File lib/logging/repository.rb, line 20
    def initialize
      @masters = []
      @h = {:root => ::Logging::RootLogger.new}

      # configures the internal logger which is disabled by default
      logger = ::Logging::Logger.allocate
      logger._setup(
          to_key(::Logging),
          :parent   => @h[:root],
          :additive => false,
          :level    => ::Logging::LEVELS.length   # turns this logger off
      )
      @h[logger.name] = logger
    end

### Public Instance Methods

instance[name] click to toggle source

Returns the `Logger` named name.

When name is a `String` or a `Symbol` it will be used "as is" to retrieve the logger. When name is a `Class` the class name will be used to retrieve the logger. When name is an object the name of the object's class will be used to retrieve the logger.

Example:

    repo = Repository.instance
    obj = MyClass.new

    log1 = repo[obj]
    log2 = repo[MyClass]
    log3 = repo['MyClass']

    log1.object_id == log2.object_id         # => true
    log2.object_id == log3.object_id         # => true

    # File lib/logging/repository.rb, line 57
    def []( key ) @h[to_key(key)] end

instance[name] = logger click to toggle source

Stores the logger under the given name.

When name is a `String` or a `Symbol` it will be used "as is" to store the logger. When name is a `Class` the class name will be used to store the logger. When name is an object the name of the object's class will be used to store the logger.

    # File lib/logging/repository.rb, line 69
    def []=( key, val ) @h[to_key(key)] = val end

add_master( 'First::Name', 'Second::Name', ... ) click to toggle source

Add the given logger names to the list of consolidation masters. All classes in the given namespace(s) will use these loggers instead of creating their own individual loggers.

    # File lib/logging/repository.rb, line 177
    def add_master( *args )
      args.map do |key|
        key = to_key(key)
        @masters << key unless @masters.include? key
        key
      end
    end

children( key ) click to toggle source

Returns an array of the children loggers for the logger identified by key where key follows the same identification rules described in +Repository#[]+. Children are returned regardless of the existence of the logger referenced by key.

    # File lib/logging/repository.rb, line 124
    def children( parent )
      ary = []
      parent = to_key(parent)

      @h.each_pair do |child,logger|
        next if :root == child
        ary << logger if parent == parent_name(child)
      end
      return ary.sort
    end

fetch( name ) click to toggle source

Returns the `Logger` named name. An `IndexError` will be raised if the logger does not exist.

When name is a `String` or a `Symbol` it will be used "as is" to retrieve the logger. When name is a `Class` the class name will be used to retrieve the logger. When name is an object the name of the object's class will be used to retrieve the logger.

    # File lib/logging/repository.rb, line 82
    def fetch( key ) @h.fetch(to_key(key)) end

has_logger?( name ) click to toggle source

Returns `true` if the given logger exists in the repository. Returns `false` if this is not the case.

When name is a `String` or a `Symbol` it will be used "as is" to retrieve the logger. When name is a `Class` the class name will be used to retrieve the logger. When name is an object the name of the object's class will be used to retrieve the logger.

    # File lib/logging/repository.rb, line 95
    def has_logger?( key ) @h.has_key?(to_key(key)) end

master_for( key ) click to toggle source

Returns the consolidation master name for the given key. If there is no consolidation master, then `nil` is returned.

    # File lib/logging/repository.rb, line 191
    def master_for( key )
      return if @masters.empty?
      key = to_key(key)

      loop do
        break key if @masters.include? key
        break nil if :root == key

        if index = key.rindex(PATH_DELIMITER)
          key = key.slice(0, index)
        else
          key = :root
        end
      end
    end

parent( key ) click to toggle source

Returns the parent logger for the logger identified by key where key follows the same identification rules described in `Repository#[]`. A parent is returned regardless of the existence of the logger referenced by key.

A note about parents -

If you have a class A::B::C, then the parent of C is B, and the parent of B is A. Parents are determined by namespace.

    # File lib/logging/repository.rb, line 110
    def parent( key )
      name = parent_name(to_key(key))
      return if name.nil?
      @h[name]
    end

parent_name( key ) click to toggle source

Returns the name of the parent for the logger identified by the given key. If the key is for the root logger, then `nil` is returned.

    # File lib/logging/repository.rb, line 158
    def parent_name( key )
      return if :root == key

      a = key.split PATH_DELIMITER
      p = :root
      while a.slice!(-1) and !a.empty?
        k = a.join PATH_DELIMITER
        if @h.has_key? k then p = k; break end
      end
      p
    end

to_key( key ) click to toggle source

Takes the given key and converts it into a form that can be used to retrieve a logger from the `Repository` hash.

When key is a `String` or a `Symbol` it will be returned "as is". When key is a `Class` the class name will be returned. When key is an object the name of the object's class will be returned.

    # File lib/logging/repository.rb, line 145
    def to_key( key )
      case key
      when :root, 'root'; :root
      when String; key
      when Symbol; key.to_s
      when Module; key.logger_name
      when Object; key.class.logger_name
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.