Sha256: 3837f5013a1007e4eaf2244357fa4dab5eb3335de6af668e7de4e51e95340b7c

Contents?: true

Size: 1.6 KB

Versions: 8

Compression:

Stored size: 1.6 KB

Contents

require 'getaround_utils/log_formatters/deep_key_value'

module GetaroundUtils; end
module GetaroundUtils::Patches; end

##
# Augment the ActiveRecord::TaggedLogging tag formatting
#
# Tags are defined either as String, Proc or a property of request
#
# Originally they will be formatted as `[tag_value]`
# This patch will instead attempt to serialize the tag with GetaroundUtils::DeepKeyValueSerializer
#
# ie:
#  - for a String `value` it would yield `"value"`
#  - for a Symbol `:request_id` it would yield `request_id="request_id_value"`
#  - for a Proc `-> {key: :val}` it would yield `key="value"`

class GetaroundUtils::Patches::KeyValueLogTags
  module TaggedLoggingFormatter
    include GetaroundUtils::LogFormatters::DeepKeyValue::Shared

    def tags_text
      "#{current_tags.join(' ')} " if current_tags.any?
    end

    def call(severity, datetime, appname, message)
      original_method = method(__method__).super_method.super_method
      message = "#{tags_text}#{normalize(message)}"
      original_method.call(severity, datetime, appname, message)
    end
  end

  module RackLogger
    def compute_tags(request)
      @taggers.collect do |tag|
        case tag
        when Proc
          GetaroundUtils::Utils::DeepKeyValue.serialize(tag.call(request))
        when Symbol
          GetaroundUtils::Utils::DeepKeyValue.serialize(tag => request.send(tag))
        else
          GetaroundUtils::Utils::DeepKeyValue.serialize(tag)
        end
      end
    end
  end

  def self.enable
    ActiveSupport::TaggedLogging::Formatter.prepend TaggedLoggingFormatter
    Rails::Rack::Logger.prepend RackLogger
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
getaround_utils-0.1.20 lib/getaround_utils/patches/key_value_log_tags.rb
getaround_utils-0.1.19 lib/getaround_utils/patches/key_value_log_tags.rb
getaround_utils-0.1.18 lib/getaround_utils/patches/key_value_log_tags.rb
getaround_utils-0.1.17 lib/getaround_utils/patches/key_value_log_tags.rb
getaround_utils-0.1.15 lib/getaround_utils/patches/key_value_log_tags.rb
getaround_utils-0.1.14 lib/getaround_utils/patches/key_value_log_tags.rb
getaround_utils-0.1.13 lib/getaround_utils/patches/key_value_log_tags.rb
getaround_utils-0.1.12 lib/getaround_utils/patches/key_value_log_tags.rb