Class: RightSupport::Rack::RequestLogger
- 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
-
#call(env) ⇒ Object
Add a logger to the Rack environment and call the next middleware.
-
#initialize(app, options = {}) ⇒ RequestLogger
constructor
Initialize an instance of the middleware, optionally providing a whitelist (:only) or a blacklist (:except) of path regexps to which request logging will apply.
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.
46 47 48 49 50 51 52 |
# File 'lib/right_support/rack/request_logger.rb', line 46 def initialize(app, = {}) @app = app @only = [:only] || [] @except = [:except] || [] # by default we don't log the query string contents because it may have sensitive data. @include_query_string = [:include_query_string] || false end |
Instance Method Details
#call(env) ⇒ Object
Add a logger to the Rack environment and call the next 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 |