Class: Puma::TCPLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/puma/tcp_logger.rb

Constant Summary collapse

FORMAT =
"%s - %s"

Instance Method Summary collapse

Constructor Details

#initialize(logger, app, quiet = false) ⇒ TCPLogger

Returns a new instance of TCPLogger.



3
4
5
6
7
# File 'lib/puma/tcp_logger.rb', line 3

def initialize(logger, app, quiet=false)
  @logger = logger
  @app = app
  @quiet = quiet
end

Instance Method Details

#call(env, socket) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/puma/tcp_logger.rb', line 17

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

#log(who, str) ⇒ Object



11
12
13
14
15
# File 'lib/puma/tcp_logger.rb', line 11

def log(who, str)
  now = Time.now.strftime("%d/%b/%Y %H:%M:%S")

  @logger.puts "#{now} - #{who} - #{str}"
end