Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
uoy-faculty-sinatra / lib / rack / combined_logger.rb
Size: Mime:
# frozen_string_literal: true

require 'rack/common_logger'

module Rack
  # Set up a logger for the Apache combined log format
  class CombinedLogger < CommonLogger
    FORMAT = %(%s - %s [%s] "%s %s%s%s %s" %d %s %s %s %0.4f\n)

    private

    # Log the request to the configured logger.
    def log(env, status, response_headers, began_at) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
      request = Rack::Request.new(env)
      length = extract_content_length(response_headers)

      username = response_headers.delete 'X-Username'
      msg = format(FORMAT,
                   request.ip || '-',
                   username || request.get_header('REMOTE_USER') || '-',
                   Time.now.strftime('%d/%b/%Y:%H:%M:%S %z'),
                   request.request_method,
                   request.script_name,
                   request.path_info,
                   request.query_string.empty? ? '' : "?#{request.query_string}",
                   request.get_header(SERVER_PROTOCOL),
                   status.to_s[0..3],
                   length,
                   request.referrer.to_s.inspect,
                   request.user_agent.to_s.inspect,
                   Utils.clock_time - began_at)

      msg.gsub!(/[^[:print:]\n]/) { |c| format('\\x%x', c.ord) }

      logger = @logger || request.get_header(RACK_ERRORS)
      # Standard library logger doesn't support write but it supports << which actually
      # calls to write on the log device without formatting
      if logger.respond_to?(:write)
        logger.write(msg)
      else
        logger << msg
      end
    end
  end
end