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) ⇒ LogWriter

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

[ GitHub ]

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

def initialize(stdout, stderr)
  @formatter = DefaultFormatter.new
  @stdout = stdout
  @stderr = stderr

  @debug = ENV.key?('PUMA_DEBUG')
  @error_logger = ErrorLogger.new(@stderr)
end

Class Method Details

.null

[ GitHub ]

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

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

.stdio

[ GitHub ]

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

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

.strings

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

[ GitHub ]

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

def self.strings
  LogWriter.new(StringIO.new, StringIO.new)
end

Instance Attribute Details

#formatter (rw)

[ GitHub ]

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

attr_accessor :formatter

#stderr (readonly)

[ GitHub ]

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

attr_reader :stdout,
            :stderr

#stdout (readonly)

[ GitHub ]

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

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 95

def connection_error(error, req, text="HTTP connection error")
  @error_logger.info(error: error, req: req, text: text)
end

#debug(str)

[ GitHub ]

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

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 127

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 82

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

#format(str)

[ GitHub ]

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

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

#internal_write (private)

[ GitHub ]

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

def internal_write
  @stdout.is_a?(IO) and @stdout.wait_writable(1)
  yield
  @stdout.flush unless @stdout.sync
rescue Errno::EPIPE, Errno::EBADF
end

#log(str)

Write str to @stdout

[ GitHub ]

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

def log(str)
  if @stdout.respond_to? :puts
    internal_write { @stdout.puts format(str) }
  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 102

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 109

def ssl_error(error, ssl_socket)
  peeraddr = ssl_socket.peeraddr.last rescue "<unknown>"
  peercert = ssl_socket.peercert
  subject = peercert ? peercert.subject : nil
  @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 119

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 65

def write(str)
  internal_write { @stdout.write format(str) }
end