Class: VCAP::Logging::SinkMap

Inherits:
Object
  • Object
show all
Defined in:
lib/vcap/logging/sink_map.rb

Instance Method Summary collapse

Constructor Details

#initialize(log_levels) ⇒ SinkMap

Returns a new instance of SinkMap.

Parameters:

  • log_levels

    Hash Map of level_name => value



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

Parameters:

  • start_level

    Symbol The most noisy level you want this sink to apply to. Use nil to set no restriction.

  • end_level

    Symbol The least noisy level you want this sink to apply to. Use nil to set no restriction.

  • sink

    BaseSink The sink to add

Raises:

  • (ArgumentError)


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_sinkObject



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.

Parameters:

  • level

    :Symbol Log level to retrieve sinks for

Returns:

  • Array



43
44
45
# File 'lib/vcap/logging/sink_map.rb', line 43

def get_sinks(level)
  @sinks[level]
end