class Sw::LogFormatter::JsonFormatter

Public Class Methods

new(*) click to toggle source
# File lib/sw/log_formatter/json_formatter.rb, line 9
def initialize(*)
  super
  self.datetime_format ||= "%Y-%m-%dT%H:%M:%S.%6N"
end

Public Instance Methods

call(severity, time, progname, msg) click to toggle source
# File lib/sw/log_formatter/json_formatter.rb, line 14
def call(severity, time, progname, msg)
  payload = context.merge(
    severity: severity,
    timestamp: format_datetime(time),
    progName: progname,
    tags: tags,
    message: msg
  )

  JSON.dump(remove_blank_values(payload)) + "\n"
end

Private Instance Methods

remove_blank_values(payload) click to toggle source
# File lib/sw/log_formatter/json_formatter.rb, line 28
def remove_blank_values(payload)
  payload.select do |_, value|
    next if value.nil?

    value = value.strip if value.respond_to?(:strip)
    next if value.respond_to?(:empty?) && value.empty?

    true
  end
end