1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
# Logging
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
```ruby
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:
```ruby
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:
```ruby
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".
```ruby
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:
```ruby
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:
```ruby
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.
```ruby
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
```
|