Class: Puma::ErrorLogger

Inherits:
Object
  • Object
show all
Includes:
Const
Defined in:
lib/puma/error_logger.rb

Overview

The implementation of a detailed error logging.

Version:

  • 5.0.0

Constant Summary collapse

REQUEST_FORMAT =
%{"%s %s%s" - (%s)}
LOG_QUEUE =
Queue.new

Constants included from Const

Const::BANNED_HEADER_KEY, Const::CGI_VER, Const::CHUNKED, Const::CHUNK_SIZE, Const::CLOSE, Const::CLOSE_CHUNKED, Const::CODE_NAME, Const::COLON, Const::CONNECTION_CLOSE, Const::CONNECTION_KEEP_ALIVE, Const::CONTENT_LENGTH, Const::CONTENT_LENGTH2, Const::CONTENT_LENGTH_S, Const::CONTINUE, Const::DQUOTE, Const::EARLY_HINTS, Const::ERROR_RESPONSE, Const::FAST_TRACK_KA_TIMEOUT, Const::GATEWAY_INTERFACE, Const::HALT_COMMAND, Const::HEAD, Const::HIJACK, Const::HIJACK_IO, Const::HIJACK_P, Const::HTTP, Const::HTTPS, Const::HTTPS_KEY, Const::HTTP_10_200, Const::HTTP_11, Const::HTTP_11_100, Const::HTTP_11_200, Const::HTTP_CONNECTION, Const::HTTP_EXPECT, Const::HTTP_HEADER_DELIMITER, Const::HTTP_HOST, Const::HTTP_VERSION, Const::HTTP_X_FORWARDED_FOR, Const::HTTP_X_FORWARDED_PROTO, Const::HTTP_X_FORWARDED_SCHEME, Const::HTTP_X_FORWARDED_SSL, Const::IANA_HTTP_METHODS, Const::ILLEGAL_HEADER_KEY_REGEX, Const::ILLEGAL_HEADER_VALUE_REGEX, Const::KEEP_ALIVE, Const::LINE_END, Const::LOCALHOST, Const::LOCALHOST_IPV4, Const::LOCALHOST_IPV6, Const::MAX_BODY, Const::MAX_HEADER, Const::NEWLINE, Const::PATH_INFO, Const::PORT_443, Const::PORT_80, Const::PROXY_PROTOCOL_V1_REGEX, Const::PUMA_CONFIG, Const::PUMA_PEERCERT, Const::PUMA_SERVER_STRING, Const::PUMA_SOCKET, Const::PUMA_TMP_BASE, Const::PUMA_VERSION, Const::QUERY_STRING, Const::RACK_AFTER_REPLY, Const::RACK_INPUT, Const::RACK_URL_SCHEME, Const::REMOTE_ADDR, Const::REQUEST_METHOD, Const::REQUEST_PATH, Const::REQUEST_URI, Const::RESTART_COMMAND, Const::SERVER_NAME, Const::SERVER_PORT, Const::SERVER_PROTOCOL, Const::SERVER_SOFTWARE, Const::STOP_COMMAND, Const::SUPPORTED_HTTP_METHODS, Const::TRANSFER_ENCODING, Const::TRANSFER_ENCODING2, Const::TRANSFER_ENCODING_CHUNKED, Const::UNMASKABLE_HEADERS, Const::UNSPECIFIED_IPV4, Const::UNSPECIFIED_IPV6, Const::WRITE_TIMEOUT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ioerr) ⇒ ErrorLogger

Returns a new instance of ErrorLogger.



18
19
20
21
22
# File 'lib/puma/error_logger.rb', line 18

def initialize(ioerr)
  @ioerr = ioerr

  @debug = ENV.key? 'PUMA_DEBUG'
end

Instance Attribute Details

#ioerrObject (readonly)

Returns the value of attribute ioerr.



12
13
14
# File 'lib/puma/error_logger.rb', line 12

def ioerr
  @ioerr
end

Class Method Details

.stdioObject



24
25
26
# File 'lib/puma/error_logger.rb', line 24

def self.stdio
  new $stderr
end

Instance Method Details

#debug(options = {}) ⇒ Object

Print occurred error details only if environment variable PUMA_DEBUG is defined. options hash with additional options:

  • error is an exception object

  • req the http request

  • text (default nil) custom string to print in title and before all remaining info.



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/puma/error_logger.rb', line 47

def debug(options={})
  return unless @debug

  error = options[:error]
  req = options[:req]

  string_block = []
  string_block << title(options)
  string_block << request_dump(req) if request_parsed?(req)
  string_block << error.backtrace if error

  internal_write string_block.join("\n")
end

#info(options = {}) ⇒ Object

Print occurred error details. options hash with additional options:

  • error is an exception object

  • req the http request

  • text (default nil) custom string to print in title and before all remaining info.



35
36
37
# File 'lib/puma/error_logger.rb', line 35

def info(options={})
  internal_write title(options)
end

#request_dump(req) ⇒ Object



73
74
75
76
# File 'lib/puma/error_logger.rb', line 73

def request_dump(req)
  "Headers: #{request_headers(req)}\n" \
  "Body: #{req.body}"
end

#request_headers(req) ⇒ Object



89
90
91
92
# File 'lib/puma/error_logger.rb', line 89

def request_headers(req)
  headers = req.env.select { |key, _| key.start_with?('HTTP_') }
  headers.map { |key, value| [key[5..-1], value] }.to_h.inspect
end

#request_parsed?(req) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/puma/error_logger.rb', line 94

def request_parsed?(req)
  req && req.env[REQUEST_METHOD]
end

#request_title(req) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/puma/error_logger.rb', line 78

def request_title(req)
  env = req.env

  REQUEST_FORMAT % [
    env[REQUEST_METHOD],
    env[REQUEST_PATH] || env[PATH_INFO],
    env[QUERY_STRING] || "",
    env[HTTP_X_FORWARDED_FOR] || env[REMOTE_ADDR] || "-"
  ]
end

#title(options = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/puma/error_logger.rb', line 61

def title(options={})
  text = options[:text]
  req = options[:req]
  error = options[:error]

  string_block = ["#{Time.now}"]
  string_block << " #{text}" if text
  string_block << " (#{request_title(req)})" if request_parsed?(req)
  string_block << ": #{error.inspect}" if error
  string_block.join('')
end