Class: Lograge::Formatters::KeyValue

Inherits:
Object
  • Object
show all
Defined in:
lib/lograge/formatters/key_value.rb

Direct Known Subclasses

L2met

Constant Summary collapse

LOGRAGE_FIELDS =
[
  :time, :method, :path, :format, :controller, :action, :status, :error,
  :duration, :view, :db, :location
]

Instance Method Summary collapse

Instance Method Details

#call(data) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/lograge/formatters/key_value.rb', line 9

def call(data)
  fields = fields_to_display(data)

  event = fields.reduce([]) do |message, key|
    next message unless data.key?(key)

    message << format(key, data[key])
    message
  end
  event.join(' ')
end

#fields_to_display(data) ⇒ Object



21
22
23
# File 'lib/lograge/formatters/key_value.rb', line 21

def fields_to_display(data)
  LOGRAGE_FIELDS + (data.keys - LOGRAGE_FIELDS)
end

#format(key, value) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/lograge/formatters/key_value.rb', line 25

def format(key, value)
  # Exactly preserve the previous output
  # Parsing this can be ambigious if the error messages contains
  # a single quote
  value = "'#{value}'" if key == :error

  # Ensure that we always have exactly two decimals
  value = Kernel.format('%.2f', value) if value.is_a? Float

  "#{key}=#{value}"
end