The Logger
middleware logs both the request and the response body and headers.
It is highly customizable and allows to mask confidential information if necessary.
Basic Usage
conn = Faraday.new(url: 'http://httpbingo.org') do |faraday|
faraday.response :logger # log requests and responses to $stdout
end
conn.get
# => INFO -- request: GET http://httpbingo.org/
# => DEBUG -- request: User-Agent: "Faraday v1.0.0"
# => INFO -- response: Status 301
# => DEBUG -- response: date: "Sun, 19 May 2019 16:05:40 GMT"
Customize the logger
By default, the Logger
middleware uses the Ruby Logger.new($stdout)
.
You can customize it to use any logger you want by providing it when you add the middleware to the stack:
conn = Faraday.new(url: 'http://httpbingo.org') do |faraday|
faraday.response :logger, MyLogger.new($stdout)
end
Include and exclude headers/bodies
By default, the logger
middleware logs only headers for security reasons, however, you can configure it
to log bodies and errors as well, or disable headers logging if you need to.
To do so, simply provide a configuration hash when you add the middleware to the stack:
conn = Faraday.new(url: 'http://httpbingo.org') do |faraday|
faraday.response :logger, nil, { headers: true, bodies: true, errors: true }
end
You can also configure the logger
middleware with a little more complex settings
like “do not log the request bodies, but log the response bodies”.
conn = Faraday.new(url: 'http://httpbingo.org') do |faraday|
faraday.response :logger, nil, { bodies: { request: false, response: true } }
end
Please note this only works with the default formatter.
Filter sensitive information
You can filter sensitive information from Faraday logs using a regex matcher:
conn = Faraday.new(url: 'http://httpbingo.org') do |faraday|
faraday.response :logger do | logger |
logger.filter(/(api_key=)([^&]+)/, '\1[REMOVED]')
end
end
conn.get('/', api_key: 'secret')
# => INFO -- request: GET http://httpbingo.org/?api_key=[REMOVED]
# => DEBUG -- request: User-Agent: "Faraday v1.0.0"
# => INFO -- response: Status 301
# => DEBUG -- response: date: "Sun, 19 May 2019 16:12:36 GMT"
Change log level
By default, the logger
middleware logs on the info
log level. It is possible to configure
the severity by providing the log_level
option:
conn = Faraday.new(url: 'http://httpbingo.org') do |faraday|
faraday.response :logger, nil, { bodies: true, log_level: :debug }
end
Customize the formatter
You can also provide a custom formatter to control how requests, responses and errors are logged.
Any custom formatter MUST implement the request
and response
method, with one argument which
will be passed being the Faraday environment.
Any custom formatter CAN implement the exception
method,
with one argument which will be passed being the exception (StandardError).
If you make your formatter inheriting from Faraday::Logging::Formatter
,
then the methods debug
, info
, warn
, error
and fatal
are automatically delegated to the logger.
class MyFormatter < Faraday::Logging::Formatter
def request(env)
# Build a custom message using `env`
info('Request') { 'Sending Request' }
end
def response(env)
# Build a custom message using `env`
info('Response') { 'Response Received' }
end
def exception(exc)
# Build a custom message using `exc`
info('Error') { 'Error Raised' }
end
end
conn = Faraday.new(url: 'http://httpbingo.org/api_key=s3cr3t') do |faraday|
faraday.response :logger, nil, formatter: MyFormatter
end