Class: Kiev::Rack::RequestLogger

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

Constant Summary collapse

ERROR_STATUS =
500
ERROR_HEADERS =
[].freeze
ERROR_BODY =
[""].freeze
LOG_ERROR =
"ERROR"

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RequestLogger

Returns a new instance of RequestLogger.



13
14
15
# File 'lib/kiev/rack/request_logger.rb', line 13

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/kiev/rack/request_logger.rb', line 17

def call(env)
  rescued_exception = nil
  began_at = Time.now.to_f

  request = ::Rack::Request.new(env)

  begin
    status, headers, body = @app.call(env)
  rescue Exception => e
    rescued_exception = e

    status = ERROR_STATUS
    headers = ERROR_HEADERS
    body = ERROR_BODY

    if defined?(ActionDispatch::ExceptionWrapper)
      status = ::ActionDispatch::ExceptionWrapper.status_code_for_exception(rescued_exception.class.name)
    end
  end

  response = ::Rack::Response.new(body, status, headers)

  rack_exception = log_rack_exception?(env[SINATRA_ERROR]) ? env[SINATRA_ERROR] : nil
  log_exception = RequestStore.store[:error]
  exception = rescued_exception || rack_exception || log_exception

  if exception || Config.instance.log_request_condition.call(request, response)
    Kiev.event(
      :request_finished,
      form_data(
        began_at: began_at,
        env: env,
        request: request,
        response: response,
        status: status,
        body: body,
        exception: exception
      )
    )
  end

  raise rescued_exception if rescued_exception

  [status, headers, body]
end