Sha256: 45c8a9f50e92ab0c3872945081dfd27d91ccbe95649059741d7b0a0eff3b712a

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

module GetaroundUtils; end
module GetaroundUtils::LogFormatters; end

require 'getaround_utils/utils/deep_key_value_serializer'

##
# Format logs using key=value notation
#
# This logger leverage the fact that ruby Logger does not especially expect message to be string
# It will attempt to serialize message it is a string otherwise adding it as message=value

class GetaroundUtils::LogFormatters::DeepKeyValue < GetaroundUtils::Utils::DeepKeyValueSerializer
  def call(severity, _datetime, appname, message)
    payload = { severity: severity, appname: appname }
    if message.is_a?(Hash)
      "#{serialize(payload.merge(message).compact)}\n"
    else
      "#{serialize(payload.merge(message: message.to_s).compact)}\n"
    end
  end

  module Lograge
    def call(data)
      data.compact! if data.is_a?(Hash)
      serialize(data)
    end
  end

  ##
  # Return a lograge-style LogFormatter
  #
  # This formatter will only take one argument and serialize it

  def self.for_lograge
    new.extend(Lograge)
  end

  module Sidekiq
    def call(severity, datetime, appname, message)
      payload = { tid: Thread.current['sidekiq_tid'] }
      payload.merge!(Thread.current[:sidekiq_context] || {})
      "#{super.chomp} #{serialize(sidekiq: payload.compact)}\n"
    end
  end

  ##
  # Return a sidekiq-style LogFormatter
  #
  # This formatter replicates the default Sidekiq LogFormatter behavior of merging context
  # values from the current Thread's store

  def self.for_sidekiq
    new.extend(Sidekiq)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
getaround_utils-0.1.7 lib/getaround_utils/log_formatters/deep_key_value.rb