diff --git a/docs/middleware/response/logger.md b/docs/middleware/response/logger.md index d2445fffb..4cc52e42e 100644 --- a/docs/middleware/response/logger.md +++ b/docs/middleware/response/logger.md @@ -96,7 +96,8 @@ end You can also provide a custom formatter to control how requests, responses and errors are logged. Any custom formatter MUST implement the `request` and `response` method, with one argument which will be passed being the Faraday environment. -Any custom formatter CAN implement the `error` method, with one argument which will be passed being the Faraday error. +Any custom formatter CAN implement the `exception` method, +with one argument which will be passed being the exception (StandardError). If you make your formatter inheriting from `Faraday::Logging::Formatter`, then the methods `debug`, `info`, `warn`, `error` and `fatal` are automatically delegated to the logger. @@ -112,8 +113,8 @@ class MyFormatter < Faraday::Logging::Formatter info('Response') { 'Response Received' } end - def error(error) - # Build a custom message using `error` + def exception(exc) + # Build a custom message using `exc` info('Error') { 'Error Raised' } end end diff --git a/lib/faraday/logging/formatter.rb b/lib/faraday/logging/formatter.rb index 863cdc020..ae7e6ce0a 100644 --- a/lib/faraday/logging/formatter.rb +++ b/lib/faraday/logging/formatter.rb @@ -37,16 +37,16 @@ def response(env) log_body('response', env[:body]) if env[:body] && log_body?(:response) end - def error(error) + def exception(exc) return unless log_errors? - error_log = proc { error.full_message } + error_log = proc { exc.full_message } public_send(log_level, 'error', &error_log) - log_headers('error', error.response_headers) if error.respond_to?(:response_headers) && log_headers?(:error) - return unless error.respond_to?(:response_body) && error.response_body && log_body?(:error) + log_headers('error', exc.response_headers) if exc.respond_to?(:response_headers) && log_headers?(:error) + return unless exc.respond_to?(:response_body) && exc.response_body && log_body?(:error) - log_body('error', error.response_body) + log_body('error', exc.response_body) end def filter(filter_word, filter_replacement) diff --git a/lib/faraday/response/logger.rb b/lib/faraday/response/logger.rb index 422e32b9c..d46414620 100644 --- a/lib/faraday/response/logger.rb +++ b/lib/faraday/response/logger.rb @@ -27,8 +27,8 @@ def on_complete(env) @formatter.response(env) end - def on_error(error) - @formatter.error(error) if @formatter.respond_to?(:error) + def on_error(exc) + @formatter.exception(exc) if @formatter.respond_to?(:exception) end end end diff --git a/spec/faraday/response/logger_spec.rb b/spec/faraday/response/logger_spec.rb index 674430e2e..9f16adf56 100644 --- a/spec/faraday/response/logger_spec.rb +++ b/spec/faraday/response/logger_spec.rb @@ -68,7 +68,7 @@ context 'when no route' do it 'delegates logging to the formatter' do expect(formatter).to receive(:request).with(an_instance_of(Faraday::Env)) - expect(formatter).to receive(:error).with(an_instance_of(Faraday::Adapter::Test::Stubs::NotFound)) + expect(formatter).to receive(:exception).with(an_instance_of(Faraday::Adapter::Test::Stubs::NotFound)) expect { conn.get '/noroute' }.to raise_error(Faraday::Adapter::Test::Stubs::NotFound) end