Class: Puma::ErrorLogger
- Inherits:
-
Object
- Object
- Puma::ErrorLogger
- Includes:
- Const
- Defined in:
- lib/puma/error_logger.rb
Overview
The implementation of a detailed error logging.
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
-
#ioerr ⇒ Object
readonly
Returns the value of attribute ioerr.
Class Method Summary collapse
Instance Method Summary collapse
-
#debug(options = {}) ⇒ Object
Print occurred error details only if environment variable PUMA_DEBUG is defined.
-
#info(options = {}) ⇒ Object
Print occurred error details.
-
#initialize(ioerr, env: ENV) ⇒ ErrorLogger
constructor
A new instance of ErrorLogger.
- #request_dump(req) ⇒ Object
- #request_headers(req) ⇒ Object
- #request_parsed?(req) ⇒ Boolean
- #request_title(req) ⇒ Object
- #title(options = {}) ⇒ Object
Constructor Details
#initialize(ioerr, env: ENV) ⇒ ErrorLogger
Returns a new instance of ErrorLogger.
18 19 20 21 22 |
# File 'lib/puma/error_logger.rb', line 18 def initialize(ioerr, env: ENV) @ioerr = ioerr @debug = env.key?('PUMA_DEBUG') end |
Instance Attribute Details
#ioerr ⇒ Object (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
.stdio(env: ENV) ⇒ Object
24 25 26 |
# File 'lib/puma/error_logger.rb', line 24 def self.stdio(env: ENV) new($stderr, env: env) 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(={}) return unless @debug error = [:error] req = [:req] string_block = [] string_block << title() 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(={}) internal_write title() 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
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(={}) text = [:text] req = [:req] error = [: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 |