2018-09-17 12:41:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-08-07 19:36:04 -04:00
|
|
|
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")
|
|
|
|
|
2016-06-06 09:18:28 -04:00
|
|
|
log_str = "#{now} - #{who} - #{str}"
|
|
|
|
|
|
|
|
case @logger
|
|
|
|
when IO
|
|
|
|
@logger.puts log_str
|
|
|
|
when Events
|
|
|
|
@logger.log log_str
|
|
|
|
end
|
2013-08-07 19:36:04 -04:00
|
|
|
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
|