Find file
Fetching contributors…
Cannot retrieve contributors at this time
117 lines (90 sloc) 3.22 KB


Build Status Gem Version Code Climate Dependency Status

Logs all requests made to your application in a search format to MongoDB. This gem includes a middleware to log rack requests to MongodB. It also adds a header to every response indicating an id that can be used to retrieve that request. You can combine this header and the one's added by rails to track the conversation applications are having with your application.


This gem using Mongoid to store the models. Once you install the gem, run the mongoid config generator to setup the connection.

gem 'http_log'

bundle install

bundle exec rails g mongoid:config # if you don't already have one

Example of What is Logged

  "raw_post":"{\"foo\": \"bar\"}",


You may also filter out specific requests. Requests to /assets are filters by default. You can filter requests using three different methods:

  1. Symbols (treated as file extensions)
  2. Regex (matched against the full URL)
  3. Blocks/Procs/Lambda/Things that respond_to? :call

Filters are used to exclude requests. So, if a filter returns true then that request will not be logged.

Here are some examples.

# Using Symbols
HttpLog.filters << :jpg

# Using Regex
HttpLog.filters << /private/

# Using Blocks/Procs/Lambdas/Things that resond_to? :call
# req is ActionDispatch::Request with some sugar
HttpLog.filter do |req|
  req.env # perhaps access the rack request stuff
  true if my_conditions_are_met

Adding More Log Information

You can add has many callbacks as you'd like. They are executed inside the middleware after the initial log is instantiated. Callbacks are ran, then the log is persisted.

Let's say you want to add some more headers of your own:

# config/initializers/http_log.rb
# req is an ActionDispatch::Request
# log is the HttpLog::Request instance

HttpLog.with_request |log, req|
  log.headers['Custom-Foo'] = 'bar'

You may also add your own fields by reopneing the class and using the Mongoid APi.

# config/initializers/http_log.rb
class HttpLog::Request
  field :developer_api_key, :type => String

HttpLog.with_request |log, req|
  log.developer_api_key = req.parameters['developer_api_key']