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.



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

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

Instance Method Details

#add_record(record) ⇒ Object



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

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

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

#countsObject

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



37
38
39
# File 'lib/steno/sink/counter.rb', line 37

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

#flushObject



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

def flush; end

#to_json(*_args) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/steno/sink/counter.rb', line 26

def to_json(*_args)
  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