Class: Semlogr::Rack::RequestLogger

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

Instance Method Summary collapse

Constructor Details

#initialize(app, logger: nil, path_filters: []) ⇒ RequestLogger

Returns a new instance of RequestLogger.


4
5
6
7
8
# File 'lib/semlogr/rack/request_logger.rb', line 4

def initialize(app, logger: nil, path_filters: [])
  @app = app
  @logger = logger
  @path_filters = path_filters
end

Instance Method Details

#call(env) ⇒ Object


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/semlogr/rack/request_logger.rb', line 10

def call(env)
  path = env['REQUEST_PATH']
  return @app.call(env) if filtered?(path)

  start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  status, headers, body = @app.call(env)
  finish = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  logger.info(
    'HTTP {method} {path} - {status} ({duration}s)',
    method: env['REQUEST_METHOD'],
    path: path,
    status: status,
    duration: (finish - start).round(4)
  )

  [status, headers, body]
end