Sha256: 9f1a41960d39270b0938c01ae6f3dca4ed816034d3eacc83c99bf13dceb3ae14

Contents?: true

Size: 506 Bytes

Versions: 1

Compression:

Stored size: 506 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 DateTime, Time
        v.iso8601(2)
      when Float
        sprintf("%.2f", v)
      when NilClass
        "nil"
      when String
        (/[ '",{}\[\]:=]/ =~ v) ? v.inspect : v
      else
        render(v.to_s)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yog-0.1.0 lib/yog/logfmt.rb