Class: Milemarker::Structured
- Inherits:
-
Milemarker
- Object
- Milemarker
- Milemarker::Structured
- Defined in:
- lib/milemarker/structured.rb
Overview
Milemarker for structured logging
* #create_logger! creates a logger that spits out JSON lines instead of human-centered strings
* #batch_line and #final_line return hashes of count/time/rate data
*...and are aliased to #batch_data and #final_data
Milemarker::Structured should be a drop-in replacement for Milemarker, with the above differences and of course the caveat that if you provide your own logger it should expect to deal with the hashes coming from #batch_data and #final_data
Constant Summary
Constants inherited from Milemarker
Instance Attribute Summary
Attributes inherited from Milemarker
#batch_end_time, #batch_number, #batch_size, #batch_start_time, #count, #last_batch_seconds, #last_batch_size, #logger, #name, #prev_count, #start_time
Instance Method Summary collapse
-
#batch_line ⇒ Hash
(also: #batch_data)
Hash with information about the last batch.
-
#create_logger!(*args, **kwargs) ⇒ Object
Create a logger that spits out JSON strings instead of human-oriented strings’ In addition to whatever message is passed, will always also include { level: severity, time: datetime }.
- #exception_message_hash(msg) ⇒ Object
-
#final_line ⇒ Hash
(also: #final_data)
Hash with information about the last batch.
- #other_message_hash(msg) ⇒ Object
Methods inherited from Milemarker
#_increment_and_on_batch, #batch_rate, #batch_rate_str, #batch_seconds_so_far, #final_batch_size, #incr, #increment_and_log_batch_line, #initialize, #log, #log_batch_line, #log_final_line, #on_batch, #reset_for_next_batch!, #set_milemarker!, #threadsafe_increment_and_on_batch, #threadsafify!, #total_rate, #total_rate_str, #total_seconds_so_far
Constructor Details
This class inherits a constructor from Milemarker
Instance Method Details
#batch_line ⇒ Hash Also known as: batch_data
Returns hash with information about the last batch.
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/milemarker/structured.rb', line 41 def batch_line { name: name, batch_count: last_batch_size, batch_seconds: last_batch_seconds, batch_rate: batch_rate, total_count: count, total_seconds: total_seconds_so_far, total_rate: total_rate } end |
#create_logger!(*args, **kwargs) ⇒ Object
Create a logger that spits out JSON strings instead of human-oriented strings’ In addition to whatever message is passed, will always also include { level: severity, time: datetime }
The logger will try to deal intelligently with different types of arguments
* a Hash will just be passed
* a String;s return json will show up in the hash under the key 'msg'
* an Exception's return json will have the error's message, class, the first bit of the backtrace, and hostname
* Anything else will be treated like a hash if it responds to #to_h;
otherwise use msg.inspect as a message string
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/milemarker/structured.rb', line 23 def create_logger!(*args, **kwargs) super @logger.formatter = proc do |severity, datetime, _progname, msg| case msg when Hash msg when String { msg: msg } when Exception (msg) else (msg) end.merge({ level: severity, time: datetime }).to_json end self end |
#exception_message_hash(msg) ⇒ Object
68 69 70 |
# File 'lib/milemarker/structured.rb', line 68 def (msg) { msg: msg., error: msg.class, at: msg.backtrace&.first, hostname: Socket.gethostname } end |
#final_line ⇒ Hash Also known as: final_data
Returns hash with information about the last batch.
56 57 58 59 60 61 62 63 64 |
# File 'lib/milemarker/structured.rb', line 56 def final_line { name: name, final_batch_size: final_batch_size, total_count: count, total_seconds: total_seconds_so_far, total_rate: total_rate } end |
#other_message_hash(msg) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/milemarker/structured.rb', line 72 def (msg) if msg.respond_to? :to_h msg.to_h else { msg: msg.inspect } end end |