Sha256: 9b3dca100c215ec9d221ab8240823064dc4f56c03c7516d72a6ecf03ec3c1846

Contents?: true

Size: 612 Bytes

Versions: 2

Compression:

Stored size: 612 Bytes

Contents

# frozen_string_literal: true

require "time"

class Yog

  # https://brandur.org/logfmt
  module Logfmt
    def self.generate(fields)
      fields.map { |k, v| "#{k}=#{render(v)}" }.join(" ")
    end

    private

    def self.render(v)
      case v
      when NilClass
        "nil"
      when TrueClass
        "true"
      when FalseClass
        "false"
      when DateTime, Time
        v.iso8601(3)
      when Float
        sprintf("%.2f", v)
      when Numeric
        v.to_s
      when String
        (/[ '",{}\[\]:=]/ =~ v) ? v.inspect : v
      else
        render(v.to_s)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
yog-0.4.0 lib/yog/logfmt.rb
yog-0.3.0 lib/yog/logfmt.rb