Class: Faraday::DetailedLogger::Middleware

Inherits:
Middleware
  • Object
show all
Defined in:
lib/faraday/detailed_logger/middleware/legacy.rb,
lib/faraday/detailed_logger/middleware/current.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.

Instance Attribute Summary collapse

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.



40
41
42
43
44
45
# File 'lib/faraday/detailed_logger/middleware/legacy.rb', line 40

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

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#tagsObject (readonly)

Returns the value of attribute tags.



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

def tags
  @tags
end

Class Method Details

.default_loggerObject

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

Returns a Logger instance.



26
27
28
29
# File 'lib/faraday/detailed_logger/middleware/legacy.rb', line 26

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.



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/faraday/detailed_logger/middleware/legacy.rb', line 54

def call(env)
  logger.tagged(*tags) do
    logger.info { formatter.request(env) }
    logger.debug { formatter.request_body(env) }
  end
  super
rescue
  logger.error do
    "#{$!.class.name} - #{$!.message} (#{$!.backtrace.first})"
  end
  raise
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.



74
75
76
77
78
79
80
81
# File 'lib/faraday/detailed_logger/middleware/legacy.rb', line 74

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

  logger.tagged(*tags) do
    log_response_status(status) { formatter.response(env) }
    logger.debug { formatter.response_body(env) }
  end
end

#on_request(env) ⇒ Object

Public: Used by Faraday as a callback hook to process a network request before it has been made.

env - A Faraday-compatible request environment.

Returns nothing.



86
87
88
89
90
91
# File 'lib/faraday/detailed_logger/middleware/current.rb', line 86

def on_request(env)
  logger.tagged(*tags) do
    logger.info { formatter.request(env) }
    logger.debug { formatter.request_body(env) }
  end
end