Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2250 from alexeevit/feature/unified-error-logging…
…-2235 Add unified detailed error logging
- Loading branch information
Showing
7 changed files
with
227 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'puma/const' | ||
|
||
module Puma | ||
# The implementation of a detailed error logging. | ||
# | ||
class ErrorLogger | ||
include Const | ||
|
||
attr_reader :ioerr | ||
|
||
REQUEST_FORMAT = %{"%s %s%s" - (%s)} | ||
|
||
def initialize(ioerr) | ||
@ioerr = ioerr | ||
@ioerr.sync = true | ||
|
||
@debug = ENV.key? 'PUMA_DEBUG' | ||
end | ||
|
||
def self.stdio | ||
new $stderr | ||
end | ||
|
||
# Print occured 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. | ||
# | ||
def info(options={}) | ||
ioerr.puts title(options) | ||
end | ||
|
||
# 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 | ||
|
||
error = options[:error] | ||
req = options[:req] | ||
|
||
string_block = [] | ||
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 | ||
|
||
def title(options={}) | ||
text = options[:text] | ||
req = options[:req] | ||
error = options[: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 | ||
|
||
def request_dump(req) | ||
"Headers: #{request_headers(req)}\n" \ | ||
"Body: #{req.body}" | ||
end | ||
|
||
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 | ||
|
||
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 | ||
|
||
def request_parsed?(req) | ||
req && req.env[REQUEST_METHOD] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
require 'puma/error_logger' | ||
require_relative "helper" | ||
|
||
class TestErrorLogger < Minitest::Test | ||
Req = Struct.new(:env, :body) | ||
|
||
def test_stdio | ||
error_logger = Puma::ErrorLogger.stdio | ||
|
||
assert_equal STDERR, error_logger.ioerr | ||
end | ||
|
||
def test_info_with_only_error | ||
_, err = capture_io do | ||
Puma::ErrorLogger.stdio.info(error: StandardError.new('ready')) | ||
end | ||
|
||
assert_match %r!#<StandardError: ready>!, err | ||
end | ||
|
||
def test_info_with_request | ||
env = { | ||
'REQUEST_METHOD' => 'GET', | ||
'PATH_INFO' => '/debug', | ||
'HTTP_X_FORWARDED_FOR' => '8.8.8.8' | ||
} | ||
req = Req.new(env, '{"hello":"world"}') | ||
|
||
_, err = capture_io do | ||
Puma::ErrorLogger.stdio.info(error: StandardError.new, req: req) | ||
end | ||
|
||
assert_match %r!\("GET /debug" - \(8\.8\.8\.8\)\)!, err | ||
end | ||
|
||
def test_info_with_text | ||
_, err = capture_io do | ||
Puma::ErrorLogger.stdio.info(text: 'The client disconnected while we were reading data') | ||
end | ||
|
||
assert_match %r!The client disconnected while we were reading data!, err | ||
end | ||
|
||
def test_debug_without_debug_mode | ||
_, err = capture_io do | ||
Puma::ErrorLogger.stdio.debug(text: 'blank') | ||
end | ||
|
||
assert_empty err | ||
end | ||
|
||
def test_debug_with_debug_mode | ||
with_debug_mode do | ||
_, err = capture_io do | ||
Puma::ErrorLogger.stdio.debug(text: 'non-blank') | ||
end | ||
|
||
assert_match %r!non-blank!, err | ||
end | ||
end | ||
|
||
private | ||
|
||
def with_debug_mode | ||
original_debug, ENV["PUMA_DEBUG"] = ENV["PUMA_DEBUG"], "1" | ||
yield | ||
ensure | ||
ENV["PUMA_DEBUG"] = original_debug | ||
end | ||
end |
Oops, something went wrong.