debian-mirror-gitlab/lib/gitlab/grape_logging/formatters/lograge_with_timestamp.rb

47 lines
1.1 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
module Gitlab
module GrapeLogging
module Formatters
class LogrageWithTimestamp
2018-11-08 19:23:39 +05:30
include Gitlab::EncodingHelper
2018-03-17 18:26:18 +05:30
def call(severity, datetime, _, data)
time = data.delete :time
2018-12-13 13:39:08 +05:30
data[:params] = process_params(data)
2018-11-08 19:23:39 +05:30
2018-03-17 18:26:18 +05:30
attributes = {
time: datetime.utc.iso8601(3),
severity: severity,
duration: time[:total],
db: time[:db],
view: time[:view]
}.merge(data)
::Lograge.formatter.call(attributes) + "\n"
end
2018-11-08 19:23:39 +05:30
private
2018-12-13 13:39:08 +05:30
def process_params(data)
return [] unless data.has_key?(:params)
data[:params]
.each_pair
.map { |k, v| { key: k, value: utf8_encode_values(v) } }
end
2018-11-08 19:23:39 +05:30
def utf8_encode_values(data)
case data
when Hash
data.merge(data) { |k, v| utf8_encode_values(v) }
when Array
data.map { |v| utf8_encode_values(v) }
when String
encode_utf8(data)
end
end
2018-03-17 18:26:18 +05:30
end
end
end
end