Sha256: 9eeed9eadbfc12da7c2692d22a84dc34f5aedf2b9df742733f1ab5d4133f7426

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

require "json"

module SemanticLogger
  module Formatters
    class Logfmt < Raw
      def initialize(time_format: :iso_8601, time_key: :timestamp, **args)
        super(time_format: time_format, time_key: time_key, **args)
      end

      def call(log, logger)
        @raw = super(log, logger)

        raw_to_logfmt
      end

      private

      def raw_to_logfmt
        @parsed = @raw.slice(:timestamp, :level, :name, :message, :duration).merge tag: "success"
        handle_payload
        handle_exception

        flatten_log
      end

      def handle_payload
        return unless @raw.key? :payload

        @parsed = @parsed.merge(@raw[:payload])
      end

      def handle_exception
        return unless @raw.key? :exception

        @parsed[:tag] = "exception"
        @parsed = @parsed.merge(@raw[:exception])
      end

      def flatten_log
        flattened = @parsed.map do |key, value|
          "#{key}=#{parse_value(value)}"
        end

        flattened.join(" ")
      end

      def parse_value(value)
        return value.to_json if value.instance_of? String

        value
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
semantic_logger-4.8.2 lib/semantic_logger/formatters/logfmt.rb
semantic_logger-4.8.1 lib/semantic_logger/formatters/logfmt.rb
semantic_logger-4.8.0 lib/semantic_logger/formatters/logfmt.rb