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/debug_logger.rb

56 lines
1.4 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
# The implementation of a logging in debug mode.
#
class DebugLogger
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
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