# 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(2) 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