class Faraday::Logging::Formatter
Serves as an integration point to customize logging
Constants
- DEFAULT_OPTIONS
Public Class Methods
Source
# File lib/faraday/logging/formatter.rb, line 13 def initialize(logger:, options:) @logger = logger @filter = [] @options = DEFAULT_OPTIONS.merge(options) end
Public Instance Methods
Source
# File lib/faraday/logging/formatter.rb, line 39 def filter(filter_word, filter_replacement) @filter.push([filter_word, filter_replacement]) end
Source
# File lib/faraday/logging/formatter.rb, line 21 def request(env) request_log = proc 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
Source
# File lib/faraday/logging/formatter.rb, line 31 def response(env) status = proc { "Status #{env.status}" } public_send(log_level, 'response', &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
Source
# File lib/faraday/logging/formatter.rb, line 79 def apply_filters(output) @filter.each do |pattern, replacement| output = output.to_s.gsub(pattern, replacement) end output end
Source
# File lib/faraday/logging/formatter.rb, line 49 def dump_body(body) if body.respond_to?(:to_str) body.to_str else pretty_inspect(body) end end
Source
# File lib/faraday/logging/formatter.rb, line 45 def dump_headers(headers) headers.map { |k, v| "#{k}: #{v.inspect}" }.join("\n") end
Source
# File lib/faraday/logging/formatter.rb, line 99 def log_body(type, body) body_log = proc { apply_filters(dump_body(body)) } public_send(log_level, type, &body_log) end
Source
# File lib/faraday/logging/formatter.rb, line 70 def log_body?(type) case @options[:bodies] when Hash @options[:bodies][type] else @options[:bodies] end end
Source
# File lib/faraday/logging/formatter.rb, line 94 def log_headers(type, headers) headers_log = proc { apply_filters(dump_headers(headers)) } public_send(log_level, type, &headers_log) end
Source
# File lib/faraday/logging/formatter.rb, line 61 def log_headers?(type) case @options[:headers] when Hash @options[:headers][type] else @options[:headers] end end
Source
# File lib/faraday/logging/formatter.rb, line 86 def log_level unless %i[debug info warn error fatal].include?(@options[:log_level]) return :info end @options[:log_level] end
Source
# File lib/faraday/logging/formatter.rb, line 57 def pretty_inspect(body) body.pretty_inspect end