Sha256: fcc1c796f55d8e699216b087e34498aa1d47926f97f023375db0d8ae3ecdb0fb

Contents?: true

Size: 721 Bytes

Versions: 3

Compression:

Stored size: 721 Bytes

Contents

# frozen_string_literal: true

require 'json'

module RabbitmqClient
  # Formatter for json log messages
  class JsonFormatter
    TIME_FORMAT = '%Y-%m-%dT%H:%M:%S.%6N '
    def initialize
      @json = {}
      @msg = ''
    end

    def call(severity, timestamp, progname, msg)
      @json = build_new_json(msg)
      @json = @json.merge(progname: progname.to_s, level: severity,
                          timestamp: timestamp.strftime(TIME_FORMAT))
      @json = @json.reject { |_key, value| value.to_s.empty? }

      @json.to_json + "\n"
    end

    def build_new_json(msg)
      @msg  = msg
      @json = @msg.is_a?(Hash) ? @msg : { message: @msg.strip }
      @json.merge(TagsFilter.tags || {})
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rabbitmq_client-0.0.3 lib/rabbitmq_client/json_formatter.rb
rabbitmq_client-0.0.2 lib/rabbitmq_client/json_formatter.rb
rabbitmq_client-0.0.1 lib/rabbitmq_client/json_formatter.rb