class Stump::AccessLog

The Middleware ensures that the logger provided (could be a standard Ruby Logger) gets called by Rack.

Constants

ACCESS_LOG_FORMAT

Adheres to the Apache Common Log format: en.wikipedia.org/wiki/Common_Log_Format

Public Class Methods

new(app, logger) click to toggle source
# File lib/stump/access_log.rb, line 12
def initialize(app, logger)
  @app = app
  @logger = logger || ::Logger.new(STDOUT, 'daily')
  @logger.level ||= 'info'
end

Public Instance Methods

call(env) click to toggle source
# File lib/stump/access_log.rb, line 18
def call(env)
  env['rack.logger'] = @logger
  began_at = Time.now
  status, header, body = @app.call(env)
  log(env, status, began_at)
  [status, header, body]
end

Private Instance Methods

log(env, status, began_at) click to toggle source

Logs access log type messages to the logger's targets if the @access_log instance variable exists

# File lib/stump/access_log.rb, line 32
def log(env, status, began_at)
  now = Time.now
  msg = ACCESS_LOG_FORMAT % [
      env['HTTP_X_FORWARDED_FOR'] || env['REMOTE_ADDR'] || '-',
      env['REMOTE_USER'] || '-',
      now.strftime('%d/%b/%Y:%H:%M:%S %z'),
      env['REQUEST_METHOD'],
      env['PATH_INFO'],
      env['QUERY_STRING'].empty? ? '' : '?'+env['QUERY_STRING'],
      env['HTTP_VERSION'],
      status.to_s[0..3],
      now - began_at ]

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