From 529b5b043e36dd73de7b0bf5a5f6dd0e09532e8e Mon Sep 17 00:00:00 2001 From: kodram Date: Mon, 9 Dec 2024 20:45:36 +1000 Subject: [PATCH] Formatting the log using parameter progname for the logger (#1606) --- lib/faraday/logging/formatter.rb | 12 ++++++------ spec/faraday/response/logger_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/faraday/logging/formatter.rb b/lib/faraday/logging/formatter.rb index 633afaadf..2fd4bb1da 100644 --- a/lib/faraday/logging/formatter.rb +++ b/lib/faraday/logging/formatter.rb @@ -23,8 +23,8 @@ def initialize(logger:, options:) def_delegators :@logger, :debug, :info, :warn, :error, :fatal def request(env) - public_send(log_level, 'request') do - "#{env.method.upcase} #{apply_filters(env.url.to_s)}" + public_send(log_level) do + "request: #{env.method.upcase} #{apply_filters(env.url.to_s)}" end log_headers('request', env.request_headers) if log_headers?(:request) @@ -32,7 +32,7 @@ def request(env) end def response(env) - public_send(log_level, 'response') { "Status #{env.status}" } + public_send(log_level) { "response: Status #{env.status}" } log_headers('response', env.response_headers) if log_headers?(:response) log_body('response', env[:body]) if env[:body] && log_body?(:response) @@ -41,7 +41,7 @@ def response(env) def exception(exc) return unless log_errors? - public_send(log_level, 'error') { exc.full_message } + public_send(log_level) { "error: #{exc.full_message}" } 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) @@ -107,11 +107,11 @@ def log_level end def log_headers(type, headers) - public_send(log_level, type) { apply_filters(dump_headers(headers)) } + public_send(log_level) { "#{type}: #{apply_filters(dump_headers(headers))}" } end def log_body(type, body) - public_send(log_level, type) { apply_filters(dump_body(body)) } + public_send(log_level) { "#{type}: #{apply_filters(dump_body(body))}" } end end end diff --git a/spec/faraday/response/logger_spec.rb b/spec/faraday/response/logger_spec.rb index 4fd1e11c4..29ed555de 100644 --- a/spec/faraday/response/logger_spec.rb +++ b/spec/faraday/response/logger_spec.rb @@ -55,6 +55,26 @@ end end + context 'when logger with program name' do + let(:logger) { Logger.new(string_io, progname: 'my_best_program') } + + it 'logs with program name' do + conn.get '/hello' + + expect(string_io.string).to match('-- my_best_program: request:') + expect(string_io.string).to match('-- my_best_program: response:') + end + end + + context 'when logger without program name' do + it 'logs without program name' do + conn.get '/hello' + + expect(string_io.string).to match('-- : request:') + expect(string_io.string).to match('-- : response:') + end + end + context 'with default formatter' do let(:formatter) { instance_double(Faraday::Logging::Formatter, request: true, response: true, filter: []) }