class Sonnet::Formatter
Public Class Methods
call(severity, time, progname, data)
click to toggle source
# File lib/sonnet/formatter.rb, line 8 def self.call(severity, time, progname, data) new(severity, time, progname, data).to_json end
current_context()
click to toggle source
# File lib/sonnet/formatter.rb, line 12 def self.current_context Thread.current[:sonnet_current_context] ||= [] end
new(severity, time, progname, data)
click to toggle source
# File lib/sonnet/formatter.rb, line 16 def initialize(severity, time, progname, data) @severity = severity @time = time @progname = progname @data = data @tags = [] end
Public Instance Methods
as_json()
click to toggle source
# File lib/sonnet/formatter.rb, line 76 def as_json { program: program, # hostname: hostname, level: level, timestamp: timestamp, pid: pid }.merge(context).merge(data).compact end
context()
click to toggle source
# File lib/sonnet/formatter.rb, line 55 def context self.class.current_context.inject({}) do |memo, context| context = context.dup tags = memo.fetch(:tags, []) + [*context.delete(:tags)].flatten.compact tag_context = tags.empty? ? {} : { tags: tags } memo.merge(context).merge(tag_context) end end
data()
click to toggle source
# File lib/sonnet/formatter.rb, line 32 def data case @data when Exception serialize_exception(@data) when Hash @data else serialize_string(@data) end end
level()
click to toggle source
# File lib/sonnet/formatter.rb, line 64 def level @severity&.downcase end
pid()
click to toggle source
def hostname
@hostname || Socket.gethostname.force_encoding('UTF-8')
end
# File lib/sonnet/formatter.rb, line 72 def pid $$ end
program()
click to toggle source
# File lib/sonnet/formatter.rb, line 24 def program @progname || File.basename($0, '.rb').split(' ')[0] end
serialize_exception(exception)
click to toggle source
# File lib/sonnet/formatter.rb, line 43 def serialize_exception(exception) { kind: exception.class.name, message: exception.to_s, stack: exception.backtrace&.slice(0, 3) } end
serialize_string(string)
click to toggle source
# File lib/sonnet/formatter.rb, line 51 def serialize_string(string) { message: string.to_s } end
timestamp()
click to toggle source
# File lib/sonnet/formatter.rb, line 28 def timestamp (@time || Time.now).utc.iso8601(3) end
to_json(opts = nil)
click to toggle source
# File lib/sonnet/formatter.rb, line 86 def to_json(opts = nil) as_json.to_json(opts) + "\n" end