class Epilog::Formatter

Constants

DEFAULT_TIME_FORMAT
SEVERITY_MAP

Attributes

datetime_format[W]
filter[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/epilog/log_formatter.rb, line 17
def initialize(options = {})
  @filter = options[:filter] || Filter::Blacklist.new
end

Public Instance Methods

call(severity, time, progname, msg) click to toggle source
# File lib/epilog/log_formatter.rb, line 21
def call(severity, time, progname, msg)
  log = base_log(severity, time, progname)
  log.merge!(context)
  log.merge!(message(msg))

  if log[:exception].is_a?(Exception)
    log[:exception] = format_error(log[:exception])
  end

  log = before_write(log)
  "#{JSON.dump(log)}\n"
end
datetime_format() click to toggle source
# File lib/epilog/log_formatter.rb, line 34
def datetime_format
  @datetime_format || DEFAULT_TIME_FORMAT
end

Private Instance Methods

base_log(severity, time, progname) click to toggle source
# File lib/epilog/log_formatter.rb, line 40
def base_log(severity, time, progname)
  {
    timestamp: time.strftime(datetime_format),
    severity: SEVERITY_MAP[severity] || severity,
    source: progname
  }
end
before_write(log) click to toggle source
# File lib/epilog/log_formatter.rb, line 66
def before_write(log)
  @filter ? @filter.call(log) : log
end
format_error(error) click to toggle source
# File lib/epilog/log_formatter.rb, line 55
def format_error(error)
  hash = {
    name: error.class.name,
    message: error.message,
    trace: error.backtrace
  }
  cause = error.cause
  hash[:parent] = format_error(cause) unless cause.nil?
  hash
end
message(msg) click to toggle source
# File lib/epilog/log_formatter.rb, line 48
def message(msg)
  return { message: msg.message, exception: msg } if msg.is_a?(Exception)
  return msg.to_h if msg.respond_to?(:to_h)

  { message: msg.to_s }
end