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.
|
|
|
|
# +options+ hash with additional options:
|
|
|
|
# - +force+ (default nil) to log info even if debug mode is turned off
|
2020-05-09 11:04:04 -04:00
|
|
|
# - +error+ is an exception object
|
|
|
|
# - +env+ the request
|
|
|
|
# - +text+ (default nil) custom string to print in title
|
2020-05-04 14:30:18 -04:00
|
|
|
# and before all remaining info.
|
|
|
|
#
|
2020-05-09 11:04:04 -04:00
|
|
|
def error_dump(options={})
|
2020-05-04 14:30:18 -04:00
|
|
|
return unless @debug || options[:force]
|
2020-05-09 11:04:04 -04:00
|
|
|
error = options[:error]
|
|
|
|
env = options[:env]
|
|
|
|
text = options[:text]
|
2020-05-04 14:30:18 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# TODO: add all info we have about request
|
|
|
|
#
|
|
|
|
|
2020-05-09 11:04:04 -04:00
|
|
|
string_block = []
|
|
|
|
formatted_text = " #{text}:" if text
|
|
|
|
formatted_error = " #{error.inspect}" if error
|
2020-05-09 12:03:32 -04:00
|
|
|
string_block << "#{Time.now}#{formatted_text}#{formatted_error}"
|
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
|
|
|
|
|
2020-05-09 11:04:04 -04:00
|
|
|
string_block << error.backtrace if error
|
2020-05-04 14:30:18 -04:00
|
|
|
|
|
|
|
ioerr.puts string_block.join("\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|