Class: VCAP::Logging::SinkMap
- Inherits:
-
Object
- Object
- VCAP::Logging::SinkMap
- Defined in:
- lib/vcap/logging/sink_map.rb
Instance Method Summary collapse
-
#add_sink(start_level, end_level, sink) ⇒ Object
Adds a sink for all the levels in the supplied range.
- #each_sink ⇒ Object
-
#get_sinks(level) ⇒ Object
Array.
-
#initialize(log_levels) ⇒ SinkMap
constructor
A new instance of SinkMap.
Constructor Details
#initialize(log_levels) ⇒ SinkMap
Returns a new instance of SinkMap.
8 9 10 11 12 13 14 |
# File 'lib/vcap/logging/sink_map.rb', line 8 def initialize(log_levels) @log_levels = log_levels @sinks = {} for level in @log_levels.keys @sinks[level] = [] end end |
Instance Method Details
#add_sink(start_level, end_level, sink) ⇒ Object
Adds a sink for all the levels in the supplied range
Usage:
add_sink(nil, :debug, sink) # Add a sink for all levels up to, and including, the :debug level
add_sink(:info, :info, sink) # Add a sink for only the info level
add_sink(:warn, nil, sink) # Add a sink for all levels :warn and greater
add_sink(nil, nil, sink) # Add a sink for all levels
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/vcap/logging/sink_map.rb', line 27 def add_sink(start_level, end_level, sink) raise ArgumentError, "Unknown level #{start_level}" if start_level && !@log_levels.has_key?(start_level) raise ArgumentError, "Unknown level #{end_level}" if end_level && !@log_levels.has_key?(end_level) start_value = @log_levels[start_level] end_value = @log_levels[end_level] for level, value in @log_levels next if start_value && (value > start_value) next if end_value && (value < end_value) @sinks[level] << sink end end |
#each_sink ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/vcap/logging/sink_map.rb', line 47 def each_sink raise "You must supply a block" unless block_given? seen = Set.new for level, sinks in @sinks for sink in sinks next if seen.include?(sink) yield sink seen.add(sink) end end end |
#get_sinks(level) ⇒ Object
Returns Array.
43 44 45 |
# File 'lib/vcap/logging/sink_map.rb', line 43 def get_sinks(level) @sinks[level] end |