httprb / http

HTTP (The Gem! a.k.a. http.rb) - a fast Ruby HTTP client with a chainable API, streaming support, and timeouts
MIT License
3k stars 321 forks source link

Feature Request: Better Control of Logging for Binary Request Body / Response Body #784

Open ksylvest opened 3 months ago

ksylvest commented 3 months ago

For non-textual files any logger running w/ an INFO level causes a lot of noise / gibberish. For example, with logging and an AAC file:

11:55:13 | ??X@??libfaac 1.30?;~,?2Sx??2֡?"dI$D??:L??tY}?q????T?J?9’R?~???W?Md?????/?????????~
...

It doesn't appear to be possible to customize this outside changing the log level to never see request.body / response.body. For example:

wrap_response

Current:

logger.info { "< #{response.status}" }
logger.debug { "#{stringify_headers(response.headers)}\n\n#{response.body}" }

Proposed:

logger.info { "< #{response.status}" }
logger.debug { "#{stringify_headers(response.headers)}" }
if response.body.log?
  logger.debug { "\n\n" }
  logger.debug { String(response.body) } 
end

log? might be:

def log?
  @encoding != Encoding::BINARY
end

wrap_request

Current:

logger.info { "> #{request.verb.to_s.upcase} #{request.uri}" }
logger.debug { "#{stringify_headers(request.headers)}\n\n#{request.body.source}" }

Proposed:

logger.info { "> #{request.verb.to_s.upcase} #{request.uri}" }
logger.debug { "#{stringify_headers(request.headers)}" }
if request.body.log?
  logger.debug { "\n\n" }
  logger.debug { String(response.body) } 
end

log? might be:

def log?
  @source.is_a?(String)
end

If this is something of interest I'd be happy to look into further.