class Faraday::Logging::Formatter
Serves as an integration point to customize logging
Constants
- DEFAULT_OPTIONS
Public Class Methods
new(logger:, options:)
click to toggle source
# File lib/faraday/logging/formatter.rb, line 14 def initialize(logger:, options:) @logger = logger @options = DEFAULT_OPTIONS.merge(options) unless %i[debug info warn error fatal].include?(@options[:log_level]) @options[:log_level] = :info end @filter = [] end
Public Instance Methods
exception(exc)
click to toggle source
# File lib/faraday/logging/formatter.rb, line 41 def exception(exc) return unless log_errors? 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) log_body('error', exc.response_body) end
filter(filter_word, filter_replacement)
click to toggle source
# File lib/faraday/logging/formatter.rb, line 52 def filter(filter_word, filter_replacement) @filter.push([filter_word, filter_replacement]) end
request(env)
click to toggle source
# File lib/faraday/logging/formatter.rb, line 25 def request(env) public_send(log_level, 'request') do "#{env.method.upcase} #{apply_filters(env.url.to_s)}" end log_headers('request', env.request_headers) if log_headers?(:request) log_body('request', env[:body]) if env[:body] && log_body?(:request) end
response(env)
click to toggle source
# File lib/faraday/logging/formatter.rb, line 34 def response(env) 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) end
Private Instance Methods
apply_filters(output)
click to toggle source
# File lib/faraday/logging/formatter.rb, line 98 def apply_filters(output) @filter.each do |pattern, replacement| output = output.to_s.gsub(pattern, replacement) end output end
dump_body(body)
click to toggle source
# File lib/faraday/logging/formatter.rb, line 64 def dump_body(body) if body.respond_to?(:to_str) body.to_str else pretty_inspect(body) end end
dump_headers(headers)
click to toggle source
# File lib/faraday/logging/formatter.rb, line 58 def dump_headers(headers) return if headers.nil? headers.map { |k, v| "#{k}: #{v.inspect}" }.join("\n") end
log_body(type, body)
click to toggle source
# File lib/faraday/logging/formatter.rb, line 113 def log_body(type, body) public_send(log_level, type) { apply_filters(dump_body(body)) } end
log_body?(type)
click to toggle source
# File lib/faraday/logging/formatter.rb, line 85 def log_body?(type) case @options[:bodies] when Hash @options[:bodies][type] else @options[:bodies] end end
log_errors?()
click to toggle source
# File lib/faraday/logging/formatter.rb, line 94 def log_errors? @options[:errors] end
log_headers(type, headers)
click to toggle source
# File lib/faraday/logging/formatter.rb, line 109 def log_headers(type, headers) public_send(log_level, type) { apply_filters(dump_headers(headers)) } end
log_headers?(type)
click to toggle source
# File lib/faraday/logging/formatter.rb, line 76 def log_headers?(type) case @options[:headers] when Hash @options[:headers][type] else @options[:headers] end end
log_level()
click to toggle source
# File lib/faraday/logging/formatter.rb, line 105 def log_level @options[:log_level] end
pretty_inspect(body)
click to toggle source
# File lib/faraday/logging/formatter.rb, line 72 def pretty_inspect(body) body.pretty_inspect end