Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
H

httplog

> 后端框架
Open source

Log outgoing HTTP requests in ruby

827 stars0 likes0 views
WebsiteGitHub

About

Log outgoing HTTP requests in ruby

httplog

Log outgoing HTTP requests made from your application. Helps with debugging pesky API error responses, or just generally understanding what's going on under the hood.

Requires ruby >= 2.6.

This gem works with the following ruby modules and libraries:

  • Net::HTTP v4+
  • Ethon
  • Excon
  • OpenURI
  • Patron
  • HTTPClient
  • HTTParty
  • HTTP

These libraries are at least partially supported, where they use one of the above as adapters, but not explicitly tested - YMMV:

  • Faraday
  • Typhoeus

In theory, it should also work with any library built on top of these. But the difference between theory and practice is bigger in practice than in theory.

This is very much a development and debugging tool; it is not recommended to use this in a production environment as it is monkey-patching the respective HTTP implementations. You have been warned - use at your own risk.

Sponsors

HttpLog is kindly sponsored by SerpApi. Scrape Google and other search engines from their fast, easy, and complete API. And use httplog to debug your client if you're using Ruby They're a Ruby on Rails shop and always looking for good developers.

Installation

bash
gem install httplog

Usage

ruby
require 'httplog' # require this *after* your HTTP gem of choice

By default, this will log all outgoing HTTP requests and their responses to $stdout on DEBUG level.

Notes on content types

  • Binary data from response bodies (as indicated by the Content-Type header)is not logged.
  • Text data (text/* and most application/* types) is encoded as UTF-8, with invalid characters replaced. If you need to inspect raw non-UTF data exactly as sent over the wire, this tool is probably not for you.

Configuration

You can override the following default options:

…

If you want to use this in a Rails app, it is recommended to configure HttpLog in an initializer, as suggested by the official Rails documentation. This approach ensures that your configuration is loaded properly and only in the desired environments.

For example, you can create a file at config/initializers/httplog.rb with the following content:

ruby
# config/initializers/httplog.rb

if Rails.env.development?
  HttpLog.configure do |config|
    config.logger = Rails.logger
  end
end

If you're running a (hopefully patched) legacy Rails 3 app, you may need to set config.logger_method = :add due to its somewhat unusual logger.

You can colorize the output to make it stand out in your logfile, either with a single color for the text:

ruby
HttpLog.configure do |config|
  config.color = :red
end

Or with a color hash for text and background:

ruby
HttpLog.configure do |config|
  config.color = {color: :black, background: :yellow}
end

For more color options please refer to the rainbow documentation

Graylog logging

If you use Graylog and want to use its search features such as "benchmark:>1 AND method:PUT", you can use this configuration:

ruby
FORMATTER = Lograge::Formatters::KeyValue.new

HttpLog.configure do |config|
  config.logger            = 
  config.logger_method     = :add
  config.severity          = GELF::Levels::DEBUG
  config.graylog_formatter = FORMATTER
end

You also can use GELF Graylog format this way:

ruby
class Lograge::Formatters::Graylog2HttpLog 
      
        Test Page
      
      
        
This is the test page.

      
    

With `log_headers = true` and a parameter 'password' in the request query and headers:

    [httplog] Connecting: localhost:80
    [httplog] Sending: GET http://localhost:9292/index.html?password=[FILTERED]
    [httplog] Header: accept: *.*
    [httplog] Header: password=[FILTERED]
    [httplog] Status: 200
    [httplog] Benchmark: 0.00057 seconds
    [httplog] Response:
    
      
        Test Page
      
      
        
This is the test page.

      
    

With `compact_log` enabled, the same request might look like this:

    [httplog] GET http://localhost:9292/index.html completed with status code 200 in 0.00057 seconds

With `json_log` enabled:

    [httplog] {"method":"GET","url":"localhost:80","request_body":null, "request_headers":{"foo":"bar"}, "response_code":200,"response_body":"\n      \n        Test Page\n      \n      \n        
This is the test page.
\n      \n    ","response_headers":{"foo":"bar"},"benchmark":0.00057}

And with `json_log` *and* `compact_log` enabled:

    [httplog] {"method":"GET","url":"localhost:80","response_code":200,"benchmark":0.00057}

### Known Issues

Following are some known quirks and issues with particular libraries. If you know a workaround or have
a suggestion for a fix, please open an issue or, even better, submit a pull request!

* Requests types other than GET and POST have not been explicitly tested.
  They may or may not be logged, depending on the implementation details of the underlying library.
  If they are not for a particular library, please feel free to open an issue with the details.

* When using OpenURI, the reading of the HTTP response body is deferred,
  so it is not available for logging. This will be noted in the logging statement:

        [httplog] Connecting: localhost:80
        [httplog] Sending: GET http://localhost:9292/index.html
        [httplog] Status: 200
        [httplog] Benchmark: 0.000617 seconds
        [httplog] Response: (not available yet)

*  When using HTTPClient, the TCP connection establishment will be logged
   *after* the HTTP request and headers, due to the way HTTPClient is organized.

        [httplog] Sending: GET http://localhost:9292/index.html
        [httplog] Header: accept: */*
        [httplog] Header: foo: bar
        [httplog] Connecting: localhost:9292
        [httplog] Status: 200
        [httplog] Benchmark: 0.001562 seconds

* Also when using HTTPClient, make sure you include `httplog` **after** `httpclient` in your `Gemfile`.

* When using Ethon or Patron, and any library based on them (such as Typhoeus),
  the TCP connection is not logged (since it's established by libcurl).

* Benchmarking only covers the time between starting the HTTP request and receiving the response. It does *not* cover the time it takes to establish the TCP connection.

### Running the specs

Make sure you have the necessary dependencies installed by running `bundle install`.
Then simply run `bundle exec rspec spec`.
This will launch a simple rack server on port 9292 and run all tests locally against that server.

### Contributing

If you have any issues with or feature requests for httplog,
please [open an issue](https://github.com/trusche/httplog/issues) on GitHub
or fork the project and send a pull request. **Please include passing specs with all pull requests.**

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyapisdebugginghttphttplog

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架