Faraday: Faraday not logging POST request body

Created on 9 Apr 2013  ·  11Comments  ·  Source: lostisland/faraday

I'm using the following code to create a connection:

@connection = Faraday.new(HOST, ssl: { verify: true }) do |faraday|
  faraday.request :url_encoded
  faraday.response :logger
  faraday.adapter Faraday.default_adapter
end

Then, I'm performing a post in this way:

@connection.post do |request|
  request.url "#{my_uri.path}?#{my_uri.query}"
  request.headers['Content-Type'] = 'application/json'
  request.body = my_object.to_json
end

The request works fine. The problem is that I'm not seeing the request body in the logs, only the request url.

Most helpful comment

Since I found this page through Google: this is now supported.

require "logger"

Faraday.new do |faraday|
  faraday.response :logger, ::Logger.new(STDOUT), bodies: true
end

There are also more fine-grained settings available if you need them. See the pull request.

All 11 comments

The logger doesn't do bodies. Maybe this can be an optional feature, but by default I don't think it should ever do bodies because they can be pretty big and would inflate the log very quickly.

I think there are multiple use-cases where you would want to log the entire body. I need this too. Yes, bodies can be pretty big, but we can use log level debug and let the passed logger instance control the verbosity.

@mtarnovan Agreed. Follow the referenced pull request for updates. We might add the feature but it will have to be opt-in, and it will need to have some sort of guard to avoid dumping non-plaintext bodies (such as compressed or binary responses)

:+1: Key for debugging

Since I found this page through Google: this is now supported.

require "logger"

Faraday.new do |faraday|
  faraday.response :logger, ::Logger.new(STDOUT), bodies: true
end

There are also more fine-grained settings available if you need them. See the pull request.

This doesn't seem to work for url_encoded requests?

Hi @vemv, the order of middlewares is extremely important in Faraday.
Can you please provide your conneciton initialization code and try to explain better why it "doesn't seem to work for url_encoded requests"?
What's the output that you get and what is the expected one?

I know this is old, but did it not occur to anyone that the OP is logging the response: faraday.response :logger.

@jpickwell you use faraday.response because the logger middleware is registered as a response middleware. In reality, it logs both requests and responses 😄

just came across this; by this line it appears that you need to pass two args to see the request body:

Faraday.new do |faraday|
  faraday.response :logger, ::Logger.new(STDOUT), body: true, bodies: { request: true, response: true }
end

(unless that has changed in a more recent commit 🤔 )

@davidalpert that's not necessary, you can simply pass the bodies: true to enable them both, or use the separate values like in the example above to have more control.
I see there's a body: true option in that example, but that's not a valid option unfortunately (maybe they got the plural wrong?)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

amrrbakry picture amrrbakry  ·  4Comments

t3hk0d3 picture t3hk0d3  ·  3Comments

asf-stripe picture asf-stripe  ·  3Comments

iMacTia picture iMacTia  ·  3Comments

yusefu picture yusefu  ·  3Comments