module RooOnRails::Logfmt

A generator for the logfmt log format.

@see brandur.org/logfmt The original description of logfmt @see godoc.org/github.com/kr/logfmt The 'reference' parser

Public Class Methods

dump(hash) click to toggle source
# File lib/roo_on_rails/logfmt.rb, line 10
def dump(hash)
  return nil if hash.nil? || hash.empty?

  hash.map { |k, v| "#{k}=#{dump_value(v)}" }.join(' ')
end

Private Class Methods

dump_value(v) click to toggle source
# File lib/roo_on_rails/logfmt.rb, line 18
def dump_value(v)
  str = case v
        when String then v
        when Symbol then v.to_s
        when Array, Hash then JSON.dump(v)
        else v.respond_to?(:to_json) ? v.to_json : v.inspect
        end

  @_escape_re ||= /[[:space:]"']/
  return str unless @_escape_re =~ str
  str.inspect
end