Sha256: c5fe2112c4b66c1fa86f55ee8e50299d88041af2d64a9a1f1dc8e4e002f359f6

Contents?: true

Size: 1014 Bytes

Versions: 10

Compression:

Stored size: 1014 Bytes

Contents

require 'json'

module RooOnRails
  # A generator for the logfmt log format.
  #
  # @see https://brandur.org/logfmt The original description of logfmt
  # @see https://godoc.org/github.com/kr/logfmt The 'reference' parser
  module Logfmt
    class << self
      SPACE = 0x20
      QUOTE = 0x22
      EQUALS = 0x3d

      def dump(hash)
        return nil if hash.nil? || hash.empty?

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

      private

      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(str)
      end

      def escape(str)
        return str if ident?(str)

        escaped = str.gsub(/(["\\])/, '\\\\\1')
        %("#{escaped}")
      end

      def ident?(str)
        str.bytes.all? { |b| b > SPACE && b != EQUALS && b != QUOTE }
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
roo_on_rails-1.10.0 lib/roo_on_rails/logfmt.rb
roo_on_rails-1.9.0 lib/roo_on_rails/logfmt.rb
roo_on_rails-1.8.1 lib/roo_on_rails/logfmt.rb
roo_on_rails-1.8.0 lib/roo_on_rails/logfmt.rb
roo_on_rails-1.7.0 lib/roo_on_rails/logfmt.rb
roo_on_rails-1.6.0 lib/roo_on_rails/logfmt.rb
roo_on_rails-1.5.0 lib/roo_on_rails/logfmt.rb
roo_on_rails-1.4.0 lib/roo_on_rails/logfmt.rb
roo_on_rails-1.3.1 lib/roo_on_rails/logfmt.rb
roo_on_rails-1.3.0 lib/roo_on_rails/logfmt.rb