Skip to content

Instantly share code, notes, and snippets.

@nz
Last active December 13, 2022 14:45
Show Gist options
  • Save nz/4999556 to your computer and use it in GitHub Desktop.
Save nz/4999556 to your computer and use it in GitHub Desktop.

Debugging with net-http-spy

Ruby applications which make HTTP calls to external services such as Websolr or Bonsai Elasticsearch may find it beneficial to use the net-http-spy gem to intercept and log HTTP calls made to that service. This is a very thorough way to log all communication between your application and the external service, which is very helpful when you need to troubleshoot an issue.

Gemfile
gem 'net-http-spy'
config/initializers/net-http-spy.rb
Net::HTTP.http_logger_options = { :verbose => true, :body => true }
Net::HTTP.http_logger = Logger.new(STDOUT)

I have chosen in this example to log to STDOUT, which is ideal for Heroku apps. You may want to log to a file, such as Rails.root.join('log/net-http-spy.log'). You should refer to the net-http-spy readme on GitHub for additional information about configuring net-http-spy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment