Class: Faraday::Logging::Formatter

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/faraday/logging/formatter.rb

Overview

Serves as an integration point to customize logging

Constant Summary collapse

DEFAULT_OPTIONS =
{ headers: true, bodies: false, errors: false,
log_level: :info }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(logger:, options:) ⇒ Formatter

Returns a new instance of Formatter.



14
15
16
17
18
19
20
21
# 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

Instance Method Details

#exception(exc) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# 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) ⇒ Object



52
53
54
# File 'lib/faraday/logging/formatter.rb', line 52

def filter(filter_word, filter_replacement)
  @filter.push([filter_word, filter_replacement])
end

#request(env) ⇒ Object



25
26
27
28
29
30
31
32
# 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) ⇒ Object



34
35
36
37
38
39
# 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