Class: Lennarb::Middleware::RequestLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/lennarb/middleware/request_logger.rb

Overview

Request logger for the Lennarb framework Logs HTTP request details with color formatting

Examples:

Output:

GET /users/123 (30ms)
Status: 200 OK
Params: {"id"=>"123", "password"=>"[FILTERED]"}

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RequestLogger

Returns a new instance of RequestLogger.



12
13
14
# File 'lib/lennarb/middleware/request_logger.rb', line 12

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Array

Process the request and log information

Parameters:

  • Rack environment

Returns:

  • Rack response [status, headers, body]



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/lennarb/middleware/request_logger.rb', line 32

def call(env)
  request = Lennarb::Request.new(env)
  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  status, headers, body = @app.call(env)

  duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time

  log_request(request, status, headers, duration, env)

  [status, headers, body]
end

#logger(env = nil) ⇒ Object

Get the logger for this request.

Resolved from the app handling the request, which App#call publishes in the Rack env, so an app's configured logger is actually used. Falls back to the class-level default when there is no app in the env.

Parameters:

  • (defaults to: nil)

    Rack environment

Returns:

  • The logger



24
25
26
# File 'lib/lennarb/middleware/request_logger.rb', line 24

def logger(env = nil)
  env&.[](RACK_LENNA_APP)&.config&.logger || Lennarb::App.app.config.logger
end