class Puma::LogWriter

Handles logging concerns for both standard messages (stdout) and errors (stderr).

Constants

DEFAULT
LOG_QUEUE

Attributes

custom_logger[RW]
formatter[RW]
stderr[R]
stdout[R]

Public Class Methods

new(stdout, stderr) click to toggle source

Create a LogWriter that prints to stdout and stderr.

# File lib/puma/log_writer.rb, line 34
def initialize(stdout, stderr)
  @formatter = DefaultFormatter.new
  @custom_logger = nil
  @stdout = stdout
  @stderr = stderr

  @debug = ENV.key?('PUMA_DEBUG')
  @error_logger = ErrorLogger.new(@stderr)
end
null() click to toggle source
# File lib/puma/log_writer.rb, line 56
def self.null
  n = NullIO.new
  LogWriter.new(n, n)
end
stdio() click to toggle source
# File lib/puma/log_writer.rb, line 52
def self.stdio
  LogWriter.new($stdout, $stderr)
end
strings() click to toggle source

Returns an LogWriter object which writes its status to two StringIO objects.

# File lib/puma/log_writer.rb, line 48
def self.strings
  LogWriter.new(StringIO.new, StringIO.new)
end

Public Instance Methods

connection_error(error, req, text="HTTP connection error") click to toggle source

An HTTP connection error has occurred. error a connection exception, req the request, and text additional info @version 5.0.0

# File lib/puma/log_writer.rb, line 111
def connection_error(error, req, text="HTTP connection error")
  @error_logger.info(error: error, req: req, text: text)
end
debug(str) click to toggle source
# File lib/puma/log_writer.rb, line 93
def debug(str)
  log("% #{str}") if @debug
end
debug?() click to toggle source
# File lib/puma/log_writer.rb, line 89
def debug?
  @debug
end
debug_error(error, req=nil, text="") click to toggle source

Log occurred error debug dump. error an exception object, req the request, and text additional info @version 5.0.0

# File lib/puma/log_writer.rb, line 143
def debug_error(error, req=nil, text="")
  @error_logger.debug(error: error, req: req, text: text)
end
error(str) click to toggle source

Write str to +@stderr+

# File lib/puma/log_writer.rb, line 98
def error(str)
  @error_logger.info(text: @formatter.call("ERROR: #{str}"))
  exit 1
end
format(str) click to toggle source
# File lib/puma/log_writer.rb, line 103
def format(str)
  formatter.call(str)
end
log(str) click to toggle source

Write str to +@stdout+

# File lib/puma/log_writer.rb, line 62
def log(str)
  if @custom_logger&.respond_to?(:write)
    @custom_logger.write(format(str))
  else
    internal_write "#{@formatter.call str}\n"
  end
end
parse_error(error, req) click to toggle source

An HTTP parse error has occurred. error a parsing exception, and req the request.

# File lib/puma/log_writer.rb, line 118
def parse_error(error, req)
  @error_logger.info(error: error, req: req, text: 'HTTP parse error, malformed request')
end
ssl_error(error, ssl_socket) click to toggle source

An SSL error has occurred. @param error <Puma::MiniSSL::SSLError> @param ssl_socket <Puma::MiniSSL::Socket>

# File lib/puma/log_writer.rb, line 125
def ssl_error(error, ssl_socket)
  peeraddr = ssl_socket.peeraddr.last rescue "<unknown>"
  peercert = ssl_socket.peercert
  subject = peercert&.subject
  @error_logger.info(error: error, text: "SSL error, peer: #{peeraddr}, peer cert: #{subject}")
end
unknown_error(error, req=nil, text="Unknown error") click to toggle source

An unknown error has occurred. error an exception object, req the request, and text additional info

# File lib/puma/log_writer.rb, line 135
def unknown_error(error, req=nil, text="Unknown error")
  @error_logger.info(error: error, req: req, text: text)
end
write(str) click to toggle source
# File lib/puma/log_writer.rb, line 70
def write(str)
  internal_write @formatter.call(str)
end

Private Instance Methods

internal_write(str) click to toggle source
# File lib/puma/log_writer.rb, line 74
def internal_write(str)
  LOG_QUEUE << str
  while (w_str = LOG_QUEUE.pop(true)) do
    begin
      @stdout.is_a?(IO) and @stdout.wait_writable(1)
      @stdout.write w_str
      @stdout.flush unless @stdout.sync
    rescue Errno::EPIPE, Errno::EBADF, IOError, Errno::EINVAL
    # 'Invalid argument' (Errno::EINVAL) may be raised by flush
    end
  end
rescue ThreadError
end