Class: Hyraft::Server::WebLogger
- Inherits:
-
Object
- Object
- Hyraft::Server::WebLogger
- Defined in:
- lib/hyraft/server/middleware/web_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) ⇒ WebLogger
constructor
A new instance of WebLogger.
Constructor Details
#initialize(app) ⇒ WebLogger
Returns a new instance of WebLogger.
15 16 17 |
# File 'lib/hyraft/server/middleware/web_logger.rb', line 15 def initialize(app) @app = app end |
Instance Method Details
#call(env) ⇒ Object
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 |
# File 'lib/hyraft/server/middleware/web_logger.rb', line 19 def call(env) start_time = Time.now status, headers, response = @app.call(env) end_time = Time.now 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 puts "#{COLORS[method_color]}#{method}#{COLORS[:reset]} #{path} → #{COLORS[status_color]}#{status_code}#{COLORS[:reset]} | #{duration_ms}ms | #{content_length_kb}" [status, headers, response] end |