123456789_123456789_123456789_123456789_123456789_

Class: Puma::LogWriter

Relationships & Source Files
Namespace Children
Classes:
Inherits: Object
Defined in: lib/puma/log_writer.rb

Overview

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

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(stdout, stderr, env: ENV) ⇒ LogWriter

Create a LogWriter that prints to #stdout and #stderr.

[ GitHub ]

  
# File 'lib/puma/log_writer.rb', line 34

def initialize(stdout, stderr, env: ENV)
  @formatter = DefaultFormatter.new
  @custom_logger = nil
  @stdout = stdout
  @stderr = stderr

  @debug = env.key?('PUMA_DEBUG')
  @error_logger = ErrorLogger.new(@stderr, env: env)
end

Class Method Details

.null(env: ENV)

[ GitHub ]

  
# File 'lib/puma/log_writer.rb', line 56

def self.null(env: ENV)
  n = NullIO.new
  LogWriter.new(n, n, env: env)
end

.stdio(env: ENV)

[ GitHub ]

  
# File 'lib/puma/log_writer.rb', line 52

def self.stdio(env: ENV)
  LogWriter.new($stdout, $stderr, env: env)
end

.strings(env: ENV)

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

[ GitHub ]

  
# File 'lib/puma/log_writer.rb', line 48

def self.strings(env: ENV)
  LogWriter.new(StringIO.new, StringIO.new, env: env)
end

Instance Attribute Details

#custom_logger (rw)

[ GitHub ]

  
# File 'lib/puma/log_writer.rb', line 31

attr_accessor :formatter, :custom_logger

#debug?Boolean (readonly)

[ GitHub ]

  
# File 'lib/puma/log_writer.rb', line 89

def debug?
  @debug
end

#formatter (rw)

[ GitHub ]

  
# File 'lib/puma/log_writer.rb', line 31

attr_accessor :formatter, :custom_logger

#stderr (readonly)

[ GitHub ]

  
# File 'lib/puma/log_writer.rb', line 28

attr_reader :stdout,
            :stderr

#stdout (readonly)

[ GitHub ]

  
# File 'lib/puma/log_writer.rb', line 28

attr_reader :stdout,
            :stderr

Instance Method Details

#connection_error(error, req, text = "HTTP connection error")

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

Version:

  • 5.0.0

[ GitHub ]

  
# 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) (readonly)

[ GitHub ]

  
# File 'lib/puma/log_writer.rb', line 93

def debug(str)
  log("% #{str}") if @debug
end

#debug_error(error, req = nil, text = "")

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

Version:

  • 5.0.0

[ GitHub ]

  
# 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)

Write str to @stderr

[ GitHub ]

  
# File 'lib/puma/log_writer.rb', line 98

def error(str)
  @error_logger.info(text: @formatter.call("ERROR: #{str}"))
  exit 1
end

#format(str)

[ GitHub ]

  
# File 'lib/puma/log_writer.rb', line 103

def format(str)
  formatter.call(str)
end

#internal_write(str) (private)

[ GitHub ]

  
# 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

#log(str)

Write str to @stdout

[ GitHub ]

  
# 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)

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

[ GitHub ]

  
# 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)

An SSL error has occurred.

Parameters:

[ GitHub ]

  
# 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")

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

[ GitHub ]

  
# 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)

[ GitHub ]

  
# File 'lib/puma/log_writer.rb', line 70

def write(str)
  internal_write @formatter.call(str)
end