Class: Steno::Sink::Counter
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #add_record(record) ⇒ Object
-
#counts ⇒ Object
Provide a map of string level -> count.
- #flush ⇒ Object
-
#initialize ⇒ Counter
constructor
A new instance of Counter.
- #to_json ⇒ Object
Constructor Details
#initialize ⇒ 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 |
#counts ⇒ Object
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 |
#flush ⇒ Object
27 28 |
# File 'lib/steno/sink/counter.rb', line 27 def flush end |
#to_json ⇒ Object
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 |