Class: Steno::Sink::Counter

Inherits:
Base show all
Defined in:
lib/steno/sink/counter.rb

Instance Attribute Summary

Attributes inherited from Base

#codec

Instance Method Summary collapse

Constructor Details

#initializeCounter

Returns a new instance of Counter.



10
11
12
13
14
# File 'lib/steno/sink/counter.rb', line 10

def initialize
  # Map of String -> numeric count
  @counts = {}
  @mutex = Mutex.new
end

Instance Method Details

#add_record(record) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/steno/sink/counter.rb', line 16

def add_record(record)
  level = record.log_level.to_s

  @mutex.synchronize do
    unless @counts[level]
      @counts[level] = 0
    end
    @counts[level] += 1
  end
end

#countsObject

Provide a map of string level -> count. This is thread-safe, the return value is a copy.



41
42
43
# File 'lib/steno/sink/counter.rb', line 41

def counts
  @mutex.synchronize { @counts.dup }
end

#flushObject



27
28
# File 'lib/steno/sink/counter.rb', line 27

def flush
end

#to_jsonObject



30
31
32
33
34
35
36
37
38
# File 'lib/steno/sink/counter.rb', line 30

def to_json
  hash = {}
  @mutex.synchronize do
    Steno::Logger::LEVELS.keys.each do |level_name|
      hash[level_name] = @counts.fetch(level_name.to_s, 0)
    end
  end
  Yajl::Encoder.encode(hash)
end