Class: Faraday::DetailedLogger::Middleware

Inherits:
Response::Middleware
  • Object
show all
Defined in:
lib/faraday/detailed_logger/middleware.rb

Overview

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.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, logger = nil, *tags) ⇒ Middleware

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.



33
34
35
36
37
# File 'lib/faraday/detailed_logger/middleware.rb', line 33

def initialize(app, logger = nil, *tags)
  super(app)
  @logger = TaggedLogging.new(logger || self.class.default_logger)
  @tags = tags
end

Class Method Details

.default_loggerObject



18
19
20
21
# File 'lib/faraday/detailed_logger/middleware.rb', line 18

def self.default_logger
  require "logger"
  ::Logger.new(STDOUT)
end

Instance Method Details

#call(env) ⇒ Object

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.



46
47
48
49
50
51
52
# File 'lib/faraday/detailed_logger/middleware.rb', line 46

def call(env)
  logger.tagged(*tags) do
    logger.info { "#{env[:method].upcase} #{env[:url]}" }
    logger.debug { curl_output(env[:request_headers], env[:body]).inspect }
  end
  super
end

#on_complete(env) ⇒ Object

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.



61
62
63
64
65
66
67
68
# File 'lib/faraday/detailed_logger/middleware.rb', line 61

def on_complete(env)
  status = env[:status]

  logger.tagged(*tags) do
    log_response_status(status) { "HTTP #{status}" }
    logger.debug { curl_output(env[:response_headers], env[:body]).inspect }
  end
end