Sha256: 6031584f36e044aee14dc221169cfefbe0e45fa0471be18ee9cd4fff40a42ea8

Contents?: true

Size: 1.39 KB

Versions: 84

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module HTTParty
  module Logger
    class LogstashFormatter #:nodoc:
      TAG_NAME = HTTParty.name

      attr_accessor :level, :logger

      def initialize(logger, level)
        @logger = logger
        @level  = level.to_sym
      end

      def format(request, response)
        @request = request
        @response = response

        logger.public_send level, logstash_message
      end

      private

      attr_reader :request, :response

      def logstash_message
        {
          '@timestamp' => current_time,
          '@version' => 1,
          'content_length' => content_length || '-',
          'http_method' => http_method,
          'message' => message,
          'path' => path,
          'response_code' => response.code,
          'severity' => level,
          'tags' => [TAG_NAME],
        }.to_json
      end

      def message
        "[#{TAG_NAME}] #{response.code} \"#{http_method} #{path}\" #{content_length || '-'} "
      end

      def current_time
        Time.now.strftime('%Y-%m-%d %H:%M:%S %z')
      end

      def http_method
        @http_method ||= request.http_method.name.split('::').last.upcase
      end

      def path
        @path ||= request.path.to_s
      end

      def content_length
        @content_length ||= response.respond_to?(:headers) ? response.headers['Content-Length'] : response['Content-Length']
      end
    end
  end
end

Version data entries

84 entries across 84 versions & 3 rubygems

Version Path
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/httparty-0.21.0/lib/httparty/logger/logstash_formatter.rb
study_line-0.2.7 vendor/bundle/ruby/3.2.0/gems/httparty-0.21.0/lib/httparty/logger/logstash_formatter.rb
study_line-0.2.6 vendor/bundle/ruby/3.2.0/gems/httparty-0.21.0/lib/httparty/logger/logstash_formatter.rb
study_line-0.2.5 vendor/bundle/ruby/3.2.0/gems/httparty-0.21.0/lib/httparty/logger/logstash_formatter.rb
study_line-0.2.4 vendor/bundle/ruby/3.2.0/gems/httparty-0.21.0/lib/httparty/logger/logstash_formatter.rb
study_line-0.2.3 vendor/bundle/ruby/3.2.0/gems/httparty-0.21.0/lib/httparty/logger/logstash_formatter.rb
study_line-0.2.2 vendor/bundle/ruby/3.2.0/gems/httparty-0.21.0/lib/httparty/logger/logstash_formatter.rb
study_line-0.2.1 vendor/bundle/ruby/3.2.0/gems/httparty-0.21.0/lib/httparty/logger/logstash_formatter.rb
study_line-0.2.0 vendor/bundle/ruby/3.2.0/gems/httparty-0.21.0/lib/httparty/logger/logstash_formatter.rb
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/httparty-0.21.0/lib/httparty/logger/logstash_formatter.rb
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/httparty-0.21.0/lib/httparty/logger/logstash_formatter.rb
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/httparty-0.21.0/lib/httparty/logger/logstash_formatter.rb
harbr-0.2.7 vendor/bundle/ruby/3.2.0/gems/httparty-0.21.0/lib/httparty/logger/logstash_formatter.rb
harbr-0.2.6 vendor/bundle/ruby/3.2.0/gems/httparty-0.21.0/lib/httparty/logger/logstash_formatter.rb
harbr-0.2.5 vendor/bundle/ruby/3.2.0/gems/httparty-0.21.0/lib/httparty/logger/logstash_formatter.rb
harbr-0.2.4 vendor/bundle/ruby/3.2.0/gems/httparty-0.21.0/lib/httparty/logger/logstash_formatter.rb
harbr-0.2.3 vendor/bundle/ruby/3.2.0/gems/httparty-0.21.0/lib/httparty/logger/logstash_formatter.rb
harbr-0.2.2 vendor/bundle/ruby/3.2.0/gems/httparty-0.21.0/lib/httparty/logger/logstash_formatter.rb
harbr-0.2.1 vendor/bundle/ruby/3.2.0/gems/httparty-0.21.0/lib/httparty/logger/logstash_formatter.rb
harbr-0.2.0 vendor/bundle/ruby/3.2.0/gems/httparty-0.21.0/lib/httparty/logger/logstash_formatter.rb