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

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::FIRST_DATA_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::ILLEGAL_HEADER_KEY_REGEX, Const::ILLEGAL_HEADER_VALUE_REGEX, Const::KEEP_ALIVE, Const::LINE_END, Const::LOCALHOST, Const::LOCALHOST_IP, Const::MAX_BODY, Const::MAX_FAST_INLINE, Const::MAX_HEADER, Const::NEWLINE, Const::PATH_INFO, Const::PERSISTENT_TIMEOUT, Const::PORT_443, Const::PORT_80, 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::TRANSFER_ENCODING, Const::TRANSFER_ENCODING2, Const::TRANSFER_ENCODING_CHUNKED, Const::WORKER_CHECK_INTERVAL, 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.


16
17
18
19
20
# File 'lib/puma/error_logger.rb', line 16

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


22
23
24
# File 'lib/puma/error_logger.rb', line 22

def self.stdio
  new $stderr
end

Instance Method Details

#debug(options = {}) ⇒ Object

Print occured 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.


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

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

  log string_block.join("\n")
end

#info(options = {}) ⇒ Object

Print occured 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.


33
34
35
# File 'lib/puma/error_logger.rb', line 33

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

#request_dump(req) ⇒ Object


71
72
73
74
# File 'lib/puma/error_logger.rb', line 71

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

#request_headers(req) ⇒ Object


87
88
89
90
# File 'lib/puma/error_logger.rb', line 87

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)

92
93
94
# File 'lib/puma/error_logger.rb', line 92

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

#request_title(req) ⇒ Object


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

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


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

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