## httplog [![Gem Version](https://badge.fury.io/rb/httplog.svg)](http://badge.fury.io/rb/httplog) [![Build Status](https://travis-ci.org/trusche/httplog.svg?branch=master)](https://travis-ci.org/trusche/httplog) [![Code Climate](https://codeclimate.com/github/trusche/httplog.svg)](https://codeclimate.com/github/trusche/httplog) [![Release Version](https://img.shields.io/github/release/trusche/httplog.svg)](https://img.shields.io/github/release/trusche/httplog.svg) 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.3. This gem works with the following ruby modules and libraries: * [Net::HTTP](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/index.html) * [Ethon](https://github.com/typhoeus/ethon) * [Excon](https://github.com/geemus/excon) * [OpenURI](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/open-uri/rdoc/index.html) * [Patron](https://github.com/toland/patron) * [HTTPClient](https://github.com/nahi/httpclient) * [HTTParty](https://github.com/jnunemaker/httparty) * [HTTP](https://github.com/httprb/http) These libraries are at least partially supported, where they use one of the above as adapters, but not explicitly tested - YMMV: * [Faraday](https://github.com/technoweenie/faraday) * [Typhoeus](https://github.com/typhoeus/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. ### Installation gem install httplog ### Usage 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: ```ruby HttpLog.configure do |config| # Enable or disable all logging config.enabled = true # You can assign a different logger config.logger = Logger.new($stdout) # I really wouldn't change this... config.severity = Logger::Severity::DEBUG # Tweak which parts of the HTTP cycle to log... config.log_connect = true config.log_request = true config.log_headers = false config.log_data = true config.log_status = true config.log_response = true config.log_benchmark = true # ...or log all request as a single line by setting this to `true` config.compact_log = false # You can also log in JSON format config.json_log = false # Prettify the output - see below config.color = false # Limit logging based on URL patterns config.url_whitelist_pattern = nil config.url_blacklist_pattern = nil end ``` If you want to use this in a Rails app, I'd suggest configuring this specifically for each environment. A global initializer is not a good idea since `HttpLog` will be undefined in production. Because you're **not using this in production**, right? :) ```ruby # config/environments/development.rb HttpLog.configure do |config| config.logger = Rails.logger end ``` 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](https://github.com/sickill/rainbow) ### Compact logging If the log is too noisy for you, but you don't want to completely disable it either, set the `compact_log` option to `true`. This will log each request in a single line with method, request URI, response status and time, but no data or headers. No need to disable any other options individually. ### JSON logging If you want to log HTTP requests in a JSON format, set the `json_log` option to `true`. You can combine this with `compact_log` to only log the basic request metrics without headers and bodies. ### Example With the default configuration, the log output might look like this: D, [2012-11-21T15:09:03.532970 #6857] DEBUG -- : [httplog] Connecting: localhost:80 D, [2012-11-21T15:09:03.533877 #6857] DEBUG -- : [httplog] Sending: GET http://localhost:9292/index.html D, [2012-11-21T15:09:03.534499 #6857] DEBUG -- : [httplog] Status: 200 D, [2012-11-21T15:09:03.534544 #6857] DEBUG -- : [httplog] Benchmark: 0.00057 seconds D, [2012-11-21T15:09:03.534578 #6857] DEBUG -- : [httplog] Response: