class Ama::Logger::Formatter::Json

Public Instance Methods

call(severity, time, _progname, msg = {}) click to toggle source
# File lib/ama/logger/formatter/json.rb, line 7
def call(severity, time, _progname, msg = {})
  invalid_argument!(:msg, Hash, msg) unless msg.is_a?(Hash)

  event_name = msg.fetch(:event_name) { missing_argument!(:event_name) }
  event_source = msg.fetch(:event_source) { missing_argument!(:event_source) }
  event_id = msg.fetch(:event_id) { missing_argument!(:event_id) }

  {
    exception: msg[:exception],
    eventName: event_name,
    eventSource: event_source.to_s,
    eventId: event_id.to_s,
    eventAgent: Ama::Logger::AGENT_VERSION,
    details: details(msg),
    indexed: indexed(msg),
    severity: severity,
    timestamp: time.utc.iso8601
  }
    .compact
    .to_json
    .concat("\n")
end

Private Instance Methods

details(data = {}) click to toggle source
# File lib/ama/logger/formatter/json.rb, line 32
def details(data = {})
  data.fetch(:details) { {} }.to_json
end
indexed(opts = {}) click to toggle source
# File lib/ama/logger/formatter/json.rb, line 36
def indexed(opts = {})
  data = opts.slice(:metric_name, :metric_value, :metric_content)

  {
    metricName: data[:metric_name]&.to_s,
    metricValue: data[:metric_value]&.to_i,
    metricContent: data[:metric_content]&.to_s
  }
    .compact
end
invalid_argument!(name, klass, value) click to toggle source
# File lib/ama/logger/formatter/json.rb, line 51
def invalid_argument!(name, klass, value)
  raise ArgumentError, "expected type `#{klass.inspect}` for argument `#{name}` - got `#{value.inspect}`"
end
missing_argument!(name) click to toggle source
# File lib/ama/logger/formatter/json.rb, line 47
def missing_argument!(name)
  raise ArgumentError, "must pass the `#{name}` argument"
end