2020-05-04 14:30:18 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-05-09 10:30:48 -04:00
|
|
|
require 'puma/const'
|
|
|
|
|
2020-05-04 14:30:18 -04:00
|
|
|
module Puma
|
|
|
|
# The implementation of a logging in debug mode.
|
|
|
|
#
|
|
|
|
class DebugLogger
|
2020-05-09 10:30:48 -04:00
|
|
|
include Const
|
|
|
|
|
2020-05-04 14:30:18 -04:00
|
|
|
attr_reader :ioerr
|
|
|
|
|
|
|
|
def initialize(ioerr)
|
|
|
|
@ioerr = ioerr
|
|
|
|
@ioerr.sync = true
|
|
|
|
|
|
|
|
@debug = ENV.key? 'PUMA_DEBUG'
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.stdio
|
|
|
|
new $stderr
|
|
|
|
end
|
|
|
|
|
|
|
|
# Any error has occured during debug mode.
|
|
|
|
# +error+ is an exception object, +env+ the request,
|
|
|
|
# +options+ hash with additional options:
|
|
|
|
# - +force+ (default nil) to log info even if debug mode is turned off
|
|
|
|
# - +custom_message+ (default nil) custom string to print after title
|
|
|
|
# and before all remaining info.
|
|
|
|
#
|
|
|
|
def error_dump(error, env=nil, options={})
|
|
|
|
return unless @debug || options[:force]
|
|
|
|
|
|
|
|
#
|
|
|
|
# TODO: add all info we have about request
|
|
|
|
#
|
|
|
|
string_block = []
|
|
|
|
|
2020-05-09 10:30:48 -04:00
|
|
|
custom_message = " #{options[:custom_message]}:" if options[:custom_message]
|
|
|
|
string_block << "#{Time.now}#{custom_message} #{error.inspect}"
|
2020-05-04 14:30:18 -04:00
|
|
|
|
|
|
|
if env
|
2020-05-09 10:30:48 -04:00
|
|
|
string_block << "Handling request { #{env[REQUEST_METHOD]} #{env[REQUEST_PATH] || env[PATH_INFO]} } (#{env[HTTP_X_FORWARDED_FOR] || env[REMOTE_ADDR]})"
|
2020-05-04 14:30:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
string_block << error.backtrace
|
|
|
|
|
|
|
|
ioerr.puts string_block.join("\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|