class Faraday::DetailedLogger::Middleware

A Faraday middleware used for providing debug-level logging information. The request and response logs follow very closely with cURL output for ease of understanding.

Be careful about your log level settings when using this middleware, especially in a production environment. With a DEBUG level log enabled, there will be potential information security concerns, because the request and response headers and bodies will be logged out. At an INFO or greater level, this is not a concern.

Attributes

logger[R]
tags[R]

Public Class Methods

default_logger() click to toggle source

Internal: Used as the Middleware's logger in the case that an explicit logger is not provided.

Returns a Logger instance.

# File lib/faraday/detailed_logger/middleware.rb, line 26
def self.default_logger
  require 'logger'
  ::Logger.new($stdout)
end
new(app, logger = nil, *tags) click to toggle source

Public: Initialize a new Logger middleware.

app - A Faraday-compatible middleware stack or application. logger - A Logger-compatible object to which the log information will

be recorded.

tags - An optional array of tags to apply to the log output.

Returns a Logger instance.

Calls superclass method
# File lib/faraday/detailed_logger/middleware.rb, line 40
def initialize(app, logger = nil, *tags)
  super(app)
  @logger = TaggedLogging.new(logger || self.class.default_logger)
  @tags = tags
end

Public Instance Methods

call(env) click to toggle source

Public: Used by Faraday to execute the middleware during the request/response cycle.

env - A Faraday-compatible request environment.

Returns the result of the parent application execution.

Calls superclass method
# File lib/faraday/detailed_logger/middleware.rb, line 53
def call(env)
  logger.tagged(*tags) do
    logger.info { "#{env[:method].upcase} #{env[:url]}" }
    logger.debug { curl_request_output(env) }
  end
  super
rescue
  logger.error do
    "#{$!.class.name} - #{$!.message} (#{$!.backtrace.first})"
  end
  raise
end
on_complete(env) click to toggle source

Internal: Used by Faraday as a callback hook to process a network response after it has completed.

env - A Faraday-compatible response environment.

Returns nothing.

# File lib/faraday/detailed_logger/middleware.rb, line 73
def on_complete(env)
  status = env[:status]

  logger.tagged(*tags) do
    log_response_status(status) { "HTTP #{status}" }
    logger.debug { curl_response_output(env) }
  end
end

Private Instance Methods

curl_output(headers, body) click to toggle source
# File lib/faraday/detailed_logger/middleware.rb, line 92
def curl_output(headers, body)
  string = headers.to_a.sort_by(&:first).map { |k, v| "#{k}: #{v}" }.join("\n")
  string + "\n\n#{body}"
end
curl_request_output(env) click to toggle source
# File lib/faraday/detailed_logger/middleware.rb, line 84
def curl_request_output(env)
  curl_output(env[:request_headers], env[:body]).inspect
end
curl_response_output(env) click to toggle source
# File lib/faraday/detailed_logger/middleware.rb, line 88
def curl_response_output(env)
  curl_output(env[:response_headers], env[:body]).inspect
end
log_response_status(status, &block) click to toggle source
# File lib/faraday/detailed_logger/middleware.rb, line 97
def log_response_status(status, &block)
  case status
  when 200..399
    logger.info(&block)
  else
    logger.warn(&block)
  end
end