1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00
puma--puma/lib/puma/error_logger.rb

98 lines
2.3 KiB
Ruby
Raw Normal View History

2020-05-04 14:30:18 -04:00
# frozen_string_literal: true
require 'puma/const'
2020-05-04 14:30:18 -04:00
module Puma
2020-06-07 08:58:47 -04:00
# The implementation of a detailed error logging.
# @version 5.0.0
2020-05-04 14:30:18 -04:00
#
class ErrorLogger
include Const
2020-05-04 14:30:18 -04:00
attr_reader :ioerr
2020-05-11 06:19:53 -04:00
REQUEST_FORMAT = %{"%s %s%s" - (%s)}
2020-05-04 14:30:18 -04:00
def initialize(ioerr)
@ioerr = ioerr
@ioerr.sync = true
@debug = ENV.key? 'PUMA_DEBUG'
end
def self.stdio
new $stderr
end
# Print occured error details.
2020-05-04 14:30:18 -04:00
# +options+ hash with additional options:
2020-05-09 11:04:04 -04:00
# - +error+ is an exception object
2020-05-11 06:19:53 -04:00
# - +req+ the http request
2020-05-09 11:04:04 -04:00
# - +text+ (default nil) custom string to print in title
2020-05-04 14:30:18 -04:00
# and before all remaining info.
#
def info(options={})
2020-05-17 09:24:48 -04:00
ioerr.puts title(options)
end
2020-05-11 06:19:53 -04:00
# 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.
#
def debug(options={})
return unless @debug
2020-05-09 11:04:04 -04:00
error = options[:error]
2020-05-11 06:19:53 -04:00
req = options[:req]
2020-05-04 14:30:18 -04:00
2020-05-09 11:04:04 -04:00
string_block = []
2020-05-17 09:24:48 -04:00
string_block << title(options)
string_block << request_dump(req) if req
string_block << error_backtrace(options) if error
ioerr.puts string_block.join("\n")
end
2020-05-11 06:19:53 -04:00
2020-05-17 09:24:48 -04:00
def title(options={})
text = options[:text]
req = options[:req]
error = options[:error]
2020-05-04 14:30:18 -04:00
2020-05-17 09:24:48 -04:00
string_block = ["#{Time.now}"]
string_block << " #{text}" if text
2020-06-07 09:34:33 -04:00
string_block << " (#{request_title(req)})" if request_parsed?(req)
2020-05-17 09:24:48 -04:00
string_block << ": #{error.inspect}" if error
string_block.join('')
end
2020-05-04 14:30:18 -04:00
2020-05-17 09:24:48 -04:00
def request_dump(req)
"Headers: #{request_headers(req)}\n" \
"Body: #{req.body}"
2020-05-04 14:30:18 -04:00
end
2020-05-11 06:19:53 -04:00
2020-05-17 09:24:48 -04:00
def request_title(req)
env = req.env
REQUEST_FORMAT % [
2020-05-11 06:19:53 -04:00
env[REQUEST_METHOD],
env[REQUEST_PATH] || env[PATH_INFO],
env[QUERY_STRING] || "",
env[HTTP_X_FORWARDED_FOR] || env[REMOTE_ADDR] || "-"
]
end
2020-05-17 09:24:48 -04:00
def request_headers(req)
headers = req.env.select { |key, _| key.start_with?('HTTP_') }
2020-05-11 06:19:53 -04:00
headers.map { |key, value| [key[5..-1], value] }.to_h.inspect
end
2020-06-07 09:34:33 -04:00
def request_parsed?(req)
req && req.env[REQUEST_METHOD]
end
2020-05-04 14:30:18 -04:00
end
end