Class: RightSupport::Rack::RequestLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/right_support/rack/request_logger.rb

Overview

A Rack middleware that logs information about every HTTP request received and every exception raised while processing a request.

The middleware can be configured to use its own logger, but defaults to using env for logging if it is present. If ‘rack.logger’ is not set, this middleware will set it before calling the next middleware. Therefore, RequestLogger can be used standalone to fulfill all logging needs, or combined with Rack::Logger or another middleware that provides logging services.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RequestLogger

Initialize an instance of the middleware.

Parameters

app(Object)

the inner application or middleware layer; must respond to #call



40
41
42
# File 'lib/right_support/rack/request_logger.rb', line 40

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object

Add a logger to the Rack environment and call the next middleware.

Parameters

env(Hash)

the Rack environment

Return

always returns whatever value is returned by the next layer of middleware



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/right_support/rack/request_logger.rb', line 51

def call(env)
  logger = env["rack.logger"]

  began_at = Time.now

  log_request_begin(logger, env)
  status, header, body = @app.call(env)
  log_exception(logger, env['sinatra.error']) if env['sinatra.error']
  log_request_end(logger, status, header, began_at)

  return [status, header, body]
rescue Exception => e
  log_exception(logger, e)
  raise e
end