Class: Appydays::Loggable::RequestLogger

Inherits:
Object
  • Object
show all
Includes:
Appydays::Loggable
Defined in:
lib/appydays/loggable/request_logger.rb

Overview

Rack middleware for request logging, to replace Rack::CommonLogger. NOTE: To get additional log fields, you can subclass this and override ‘_request_tags`. If you want authenticated user info, make sure you install the middleware after your auth middleware.

Params:

error_code: If an unhandled exception reaches the request logger.

or happens in the request logger, this status code will be used for easay identification.
Generally unhandled exceptions in an application should be handled further downstream,
and already be returning the right error code.

slow_request_seconds: Normally request_finished is logged at info.

Requests that take >= this many seconds are logged at warn.

reraise: Reraise unhandled errors (after logging them).

In some cases you may want this to be true, but in some it may bring down the whole process.

Instance Method Summary collapse

Methods included from Appydays::Loggable

[], configure_12factor, default_level=, ensure_stderr_appender, included, set_default_level, with_log_tags

Constructor Details

#initialize(app, error_code: 599, slow_request_seconds: 10, reraise: true) ⇒ RequestLogger



27
28
29
30
31
32
# File 'lib/appydays/loggable/request_logger.rb', line 27

def initialize(app, error_code: 599, slow_request_seconds: 10, reraise: true)
  @app = app
  @error_code = error_code
  @slow_request_seconds = slow_request_seconds
  @reraise = reraise
end

Instance Method Details

#call(env) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/appydays/loggable/request_logger.rb', line 34

def call(env)
  began_at = Time.now
  request_fields = self._request_tags(env, began_at)
  status, header, body = SemanticLogger.named_tagged(request_fields) { @app.call(env) }
  header = Rack::Utils::HeaderHash.new(header)
  body = Rack::BodyProxy.new(body) { self.log_finished(request_fields, began_at, status, header) }
  [status, header, body]
rescue StandardError => e
  began_at ||= nil
  request_fields ||= {}
  self.log_finished(request_fields, began_at, 599, {}, e)
  raise if @reraise
end

#request_tags(_env) ⇒ Object



62
63
64
# File 'lib/appydays/loggable/request_logger.rb', line 62

def request_tags(_env)
  return {}
end