# 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