mirror of
https://github.com/puma/puma.git
synced 2022-11-09 13:48:40 -05:00
88e51fb08e
Reduces runtime allocation by freezing string literals by default. We could also remove a ton of manual `.freeze` calls, however the ruby supported version is 2.2 and the magic comment only targets 2.3+.
41 lines
808 B
Ruby
41 lines
808 B
Ruby
# frozen_string_literal: true
|
|
|
|
module Puma
|
|
class TCPLogger
|
|
def initialize(logger, app, quiet=false)
|
|
@logger = logger
|
|
@app = app
|
|
@quiet = quiet
|
|
end
|
|
|
|
FORMAT = "%s - %s"
|
|
|
|
def log(who, str)
|
|
now = Time.now.strftime("%d/%b/%Y %H:%M:%S")
|
|
|
|
log_str = "#{now} - #{who} - #{str}"
|
|
|
|
case @logger
|
|
when IO
|
|
@logger.puts log_str
|
|
when Events
|
|
@logger.log log_str
|
|
end
|
|
end
|
|
|
|
def call(env, socket)
|
|
who = env[Const::REMOTE_ADDR]
|
|
log who, "connected" unless @quiet
|
|
|
|
env['log'] = lambda { |str| log(who, str) }
|
|
|
|
begin
|
|
@app.call env, socket
|
|
rescue Object => e
|
|
log who, "exception: #{e.message} (#{e.class})"
|
|
else
|
|
log who, "disconnected" unless @quiet
|
|
end
|
|
end
|
|
end
|
|
end
|