-
Notifications
You must be signed in to change notification settings - Fork 21
/
repository.rb
70 lines (55 loc) · 1.65 KB
/
repository.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# encoding: utf-8
require 'monitor'
require "singleton"
module Yell #:nodoc:
class LoggerNotFound < StandardError
def message; "Could not find a Yell::Logger instance with the name '#{super}'"; end
end
class Repository
extend MonitorMixin
include Singleton
attr_accessor :loggers
def initialize
@loggers = {}
end
class << self
# Set loggers in the repository
#
# @example Set a logger
# Yell::Repository[ 'development' ] = Yell::Logger.new :stdout
#
# @return [Yell::Logger] The logger instance
def []=( name, logger )
synchronize { instance.loggers[name] = logger }
end
# Get loggers from the repository
#
# @example Get the logger
# Yell::Repository[ 'development' ]
#
# @raise [Yell::LoggerNotFound] Raised when repository does not have that key
# @return [Yell::Logger] The logger instance
def []( name )
synchronize { instance.fetch(name) or instance.fetch(:global) or raise Yell::LoggerNotFound.new(name) }
end
# Get the list of all loggers in the repository
#
# @return [Hash] The map of loggers
def loggers
synchronize { instance.loggers }
end
end
# Fetch the logger by the given name.
#
# If the logger could not be found and has a superclass, it
# will attempt to look there. This is important for the
# Yell::Loggable module.
def fetch( name )
logger = loggers[name] || loggers[name.to_s]
if logger.nil? && name.respond_to?(:superclass)
return fetch( name.superclass )
end
logger
end
end
end