Skip to content

Commit

Permalink
Fix some logging inefficiencies (#1515)
Browse files Browse the repository at this point in the history
  • Loading branch information
semaperepelitsa authored Jul 6, 2023
1 parent 3af5211 commit d1743e6
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions lib/faraday/logging/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,26 @@ class Formatter

def initialize(logger:, options:)
@logger = logger
@filter = []
@options = DEFAULT_OPTIONS.merge(options)
unless %i[debug info warn error fatal].include?(@options[:log_level])
@options[:log_level] = :info
end
@filter = []
end

def_delegators :@logger, :debug, :info, :warn, :error, :fatal

def request(env)
request_log = proc do
public_send(log_level, 'request') do
"#{env.method.upcase} #{apply_filters(env.url.to_s)}"
end
public_send(log_level, 'request', &request_log)

log_headers('request', env.request_headers) if log_headers?(:request)
log_body('request', env[:body]) if env[:body] && log_body?(:request)
end

def response(env)
status = proc { "Status #{env.status}" }
public_send(log_level, 'response', &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)
Expand All @@ -40,8 +41,7 @@ def response(env)
def exception(exc)
return unless log_errors?

error_log = proc { exc.full_message }
public_send(log_level, 'error', &error_log)
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)
Expand Down Expand Up @@ -103,21 +103,15 @@ def apply_filters(output)
end

def log_level
unless %i[debug info warn error fatal].include?(@options[:log_level])
return :info
end

@options[:log_level]
end

def log_headers(type, headers)
headers_log = proc { apply_filters(dump_headers(headers)) }
public_send(log_level, type, &headers_log)
public_send(log_level, type) { apply_filters(dump_headers(headers)) }
end

def log_body(type, body)
body_log = proc { apply_filters(dump_body(body)) }
public_send(log_level, type, &body_log)
public_send(log_level, type) { apply_filters(dump_body(body)) }
end
end
end
Expand Down

0 comments on commit d1743e6

Please sign in to comment.