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, options = {}) ⇒ RequestLogger

Initialize an instance of the middleware, optionally providing a whitelist (:only) or a blacklist (:except) of path regexps to which request logging will apply.

Parameters:

  • app (Object)

    inner application or middleware layer; must respond to #call

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :only (Array)

    log for these path Regexps unless in debug mode

  • :except (Array)

    log except for these path Regexps unless in debug mode; this option is ignored if :only is provided

  • :include_query_string (Array)

    log the contents of the query string



46
47
48
49
50
51
52
# File 'lib/right_support/rack/request_logger.rb', line 46

def initialize(app, options = {})
  @app = app
  @only = options[:only] || []
  @except = options[:except] || []
  # by default we don't log the query string contents because it may have sensitive data.
  @include_query_string = options[:include_query_string] || false
end

Instance Method Details

#call(env) ⇒ Object

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

Parameters:

  • env (Hash)

    Rack environment

Options Hash (env):

  • 'rack.logging.enabled' (TrueClass|FalseClass)

    in Rack environment to indicate whether logging is enabled for this request for use by other middleware in the call chain.

  • 'sinatra.error' (TrueClass|FalseClass)

    is set by Sinatra to pass error info (exception object) to handlers.

  • 'sinatra.error.handled' (TrueClass|FalseClass)

    is true to prevent any additional error handlers in the call chain from processing an error that has already handled been handled (i.e. log and response) (default = false).

Returns:

  • (Object)

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/right_support/rack/request_logger.rb', line 69

def call(env)
  logger = env["rack.logger"]
  env["rack.logging.enabled"] = enabled = logging_enabled?(logger, env)

  began_at = Time.now
  log_request_begin(logger, env) if enabled
  status, header, body = @app.call(env)
  if env['sinatra.error'] && !env['sinatra.error.handled']
    log_exception(logger, env['sinatra.error'])
  end
  log_request_end(logger, env, status, header, body, began_at) if enabled

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