Sha256: a59d32ad8bcf3bcb87aad926e04a7d2ad7aa290586cfe93dbc8ce6489a7d2b47

Contents?: true

Size: 1.94 KB

Versions: 64

Compression:

Stored size: 1.94 KB

Contents

module Inferno
  module Utils
    # @private
    module Middleware
      class RequestLogger
        attr_reader :app

        def initialize(app)
          @app = app
        end

        def verbose_logging?
          @verbose_logging ||= ENV['VERBOSE_REQUEST_LOGGING']&.downcase == true
        end

        def logger
          @logger ||= Application['logger']
        end

        def call(env)
          start = Time.now
          log_request(env)
          begin
            response = app.call(env)
            log_response(response, start, Time.now)
          rescue StandardError => e
            log_response([500, nil, nil], start, Time.now, e)
            raise e
          end

          response
        end

        def log_response(response, start_time, end_time, exception = nil)
          elapsed = end_time - start_time
          status, _response_headers, body = response if response
          status, = response if exception

          logger.info("#{status} in #{elapsed.in_milliseconds} ms")
          return unless body.present?

          body = body.is_a?(Array) ? body.join : body

          if body.length > 100 && !verbose_logging?
            logger.info("#{body[0..100]}...")
          else
            logger.info(body)
          end
        end

        def log_request(env)
          method = env['REQUEST_METHOD']
          scheme = env['rack.url_scheme']
          host = env['HTTP_HOST']
          path = env['REQUEST_URI']
          query = env['rack.request.query_string']
          body = env['rack.input']
          body = body.instance_of?(Puma::NullIO) ? nil : body.string
          query_string = query.blank? ? '' : "?#{query}"

          logger.info("#{method} #{scheme}://#{host}#{path}#{query_string}")

          return unless body.present?

          if body.length > 100 && !verbose_logging?
            logger.info("#{body[0..100]}...")
          else
            logger.info(body)
          end
        end
      end
    end
  end
end

Version data entries

64 entries across 64 versions & 1 rubygems

Version Path
inferno_core-0.4.27 lib/inferno/utils/middleware/request_logger.rb
inferno_core-0.4.26 lib/inferno/utils/middleware/request_logger.rb
inferno_core-0.4.25 lib/inferno/utils/middleware/request_logger.rb
inferno_core-0.4.24 lib/inferno/utils/middleware/request_logger.rb
inferno_core-0.4.23 lib/inferno/utils/middleware/request_logger.rb
inferno_core-0.4.22 lib/inferno/utils/middleware/request_logger.rb
inferno_core-0.4.21 lib/inferno/utils/middleware/request_logger.rb
inferno_core-0.4.20 lib/inferno/utils/middleware/request_logger.rb
inferno_core-0.4.19 lib/inferno/utils/middleware/request_logger.rb
inferno_core-0.4.18 lib/inferno/utils/middleware/request_logger.rb
inferno_core-0.4.17 lib/inferno/utils/middleware/request_logger.rb
inferno_core-0.4.16 lib/inferno/utils/middleware/request_logger.rb
inferno_core-0.4.15 lib/inferno/utils/middleware/request_logger.rb
inferno_core-0.4.14 lib/inferno/utils/middleware/request_logger.rb
inferno_core-0.4.13 lib/inferno/utils/middleware/request_logger.rb
inferno_core-0.4.12 lib/inferno/utils/middleware/request_logger.rb
inferno_core-0.4.11 lib/inferno/utils/middleware/request_logger.rb
inferno_core-0.4.10 lib/inferno/utils/middleware/request_logger.rb
inferno_core-0.4.9 lib/inferno/utils/middleware/request_logger.rb
inferno_core-0.4.8 lib/inferno/utils/middleware/request_logger.rb