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,
log_level: :info }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(logger:, options:) ⇒ Formatter

Returns a new instance of Formatter.


13
14
15
16
17
# File 'lib/faraday/logging/formatter.rb', line 13

def initialize(logger:, options:)
  @logger = logger
  @filter = []
  @options = DEFAULT_OPTIONS.merge(options)
end

Instance Method Details

#filter(filter_word, filter_replacement) ⇒ Object


39
40
41
# File 'lib/faraday/logging/formatter.rb', line 39

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

#request(env) ⇒ Object


21
22
23
24
25
26
27
28
29
# 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

#response(env) ⇒ Object


31
32
33
34
35
36
37
# 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