class Milemarker::Structured
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
Public Instance Methods
batch_line()
click to toggle source
@return [Hash] hash with information about the last batch
# 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
Also aliased as: batch_data
create_logger!(*args, **kwargs)
click to toggle source
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
Calls superclass method
Milemarker#create_logger!
# 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 exception_message_hash(msg) else other_message_hash(msg) end.merge({ level: severity, time: datetime }).to_json end self end
exception_message_hash(msg)
click to toggle source
# File lib/milemarker/structured.rb, line 68 def exception_message_hash(msg) { msg: msg.message, error: msg.class, at: msg.backtrace&.first, hostname: Socket.gethostname } end
final_line()
click to toggle source
@return [Hash] hash with information about the last batch
# 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
Also aliased as: final_data
other_message_hash(msg)
click to toggle source
# File lib/milemarker/structured.rb, line 72 def other_message_hash(msg) if msg.respond_to? :to_h msg.to_h else { msg: msg.inspect } end end