Class: RubyAPI::Middleware::StructuredLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyapi/middleware/logging.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, output: $stdout) ⇒ StructuredLogger

Returns a new instance of StructuredLogger.



7
8
9
10
# File 'lib/rubyapi/middleware/logging.rb', line 7

def initialize(app, output: $stdout)
  @app = app
  @output = output
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rubyapi/middleware/logging.rb', line 12

def call(env)
  start_time = Time.now
  status, headers, body = @app.call(env)
  duration_ms = ((Time.now - start_time) * 1000).round(2)

  log_entry = {
    method: env["REQUEST_METHOD"],
    path: env["PATH_INFO"],
    status: status,
    duration_ms: duration_ms,
    timestamp: Time.now.iso8601
  }

  @output.puts(JSON.generate(log_entry))
  [status, headers, body]
end