Module: RailsCustomLogging::Formatters::KeyValue

Defined in:
lib/rails_custom_logging/formatters/key_value.rb

Constant Summary collapse

ORDERED_HASH =

MRI trick to force the order of the payload keys with a very light cost of a single merge. MRI hash’s ‘to_a` method will return values in the order in which they are defined by implemention. `merge` follows the same pattern. If you’re using another implemention of Ruby (jRuby/truffleruby) you might end up with unordered keys.

Additional keys in the payload will be added at the end, ordering being left at your discretion.

{
  method: nil, path: nil, params: nil, format: nil, status: nil, controller: nil,
  action: nil, duration: nil, db_runtime: nil, view_runtime: nil, allocations: nil
}

Class Method Summary collapse

Class Method Details

.call(payload) ⇒ Object



22
23
24
25
26
27
# File 'lib/rails_custom_logging/formatters/key_value.rb', line 22

def call(payload)
  ordered_payload = ORDERED_HASH.merge(payload)
  ordered_payload.compact!

  ordered_payload.map { |key, value| format(key, value) }.join(' ')
end