debian-mirror-gitlab/lib/gitlab/sidekiq_logging/json_formatter.rb

42 lines
931 B
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2018-05-09 12:01:36 +05:30
module Gitlab
module SidekiqLogging
class JSONFormatter
2020-03-09 13:42:32 +05:30
TIMESTAMP_FIELDS = %w[created_at enqueued_at started_at retried_at failed_at completed_at].freeze
2018-05-09 12:01:36 +05:30
def call(severity, timestamp, progname, data)
output = {
severity: severity,
time: timestamp.utc.iso8601(3)
}
case data
when String
output[:message] = data
when Hash
2020-03-09 13:42:32 +05:30
convert_to_iso8601!(data)
2018-05-09 12:01:36 +05:30
output.merge!(data)
end
output.to_json + "\n"
end
2020-03-09 13:42:32 +05:30
private
def convert_to_iso8601!(payload)
TIMESTAMP_FIELDS.each do |key|
value = payload[key]
payload[key] = format_time(value) if value.present?
end
end
def format_time(timestamp)
return timestamp unless timestamp.is_a?(Numeric)
Time.at(timestamp).utc.iso8601(3)
end
2018-05-09 12:01:36 +05:30
end
end
end