Class: Hyraft::Server::ApiLogger
- Inherits:
-
Object
- Object
- Hyraft::Server::ApiLogger
- Defined in:
- lib/hyraft/server/middleware/api_logger.rb
Constant Summary collapse
- COLORS =
{ reset: "\e[0m", blue: "\e[34m", cyan: "\e[36m", green: "\e[32m", yellow: "\e[33m", red: "\e[31m", magenta: "\e[35m" }
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ ApiLogger
constructor
A new instance of ApiLogger.
Constructor Details
#initialize(app) ⇒ ApiLogger
Returns a new instance of ApiLogger.
14 15 16 |
# File 'lib/hyraft/server/middleware/api_logger.rb', line 14 def initialize(app) @app = app end |
Instance Method Details
#call(env) ⇒ Object
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/hyraft/server/middleware/api_logger.rb', line 18 def call(env) start_time = Time.now status, headers, response = @app.call(env) end_time = Time.now # Calculate metrics duration_ms = ((end_time - start_time) * 1000).round(2) content_length = headers['Content-Length'] || response_body_length(response) # Convert bytes to KB content_length_kb = if content_length.is_a?(Numeric) "#{(content_length / 1024.0).round(2)} KB" else content_length end path = env['PATH_INFO'] method = env['REQUEST_METHOD'] status_code = status.to_i # Method-specific colors method_color = case method when 'GET' then :green when 'POST' then :blue when 'PUT', 'PATCH' then :cyan when 'DELETE' then :red else :yellow end # Status-specific colors status_color = case status_code when 200..299 then :green when 300..399 then :cyan when 400..499 then :yellow when 500..599 then :red else :magenta end # Log with colors and metrics = "#{COLORS[method_color]}#{method}#{COLORS[:reset]} #{path} → #{COLORS[status_color]}#{status_code}#{COLORS[:reset]} | #{duration_ms}ms | #{content_length_kb}" puts [status, headers, response] end |