MongoRequestLogger

Log requests in a structured format to MongoDB.

Installation

Add this line to your application's Gemfile:

gem 'mongo_request_logger'

Usage with Rails

Create config/logger.yml containing something like the following:

development:
  host: localhost
  database: your_app_logs_production_dev
  capsize: 100  # megabytes

production:
  host: localhost
  database: your_app_logs_production
  username: your_app_logs_production
  password: password
  capsize: 1024 # megabytes

Routes for log viewer

mount MongoRequestLogger::Viewer, :at => "log"

Usage with Sinatra (or other Rack-based apps)

Config file is the same as with Rails.

In config.ru:

require 'mongo_request_logger'
require './yourapp'

# Configure database details
MongoRequestLogger.configure(File.join(File.dirname(__FILE__), 'config/logger.yml'))

# Enable the middleware to log the requests
use MongoRequestLogger::Rack

# Mount the log viewer in /log
run Rack::URLMap.new("/" => YourApp.new,
    "/log" => MongoRequestLogger::Viewer.new)

In a Sinatra app:

# This prevents Sinatra from using the default logger, so it uses our logger instead.
set :logging, nil

get '/' do
    logger.info "Logging from Sinatra!"
end

In other Rack-based apps:

env['rack.logger'].info "Logging from Rack!"

Ignoring paths

Often you want some requests not to be logged, for example assets files. This can be done by specifying the prefixes to be ignored:

MongoRequestLogger::Rack.ignore_prefixes << '/assets/'

Usage with Resque

Extend from MongoRequestLogger::LoggedJob, to have each job logged as if it was a request.

class MyJob
    extend MongoRequestLogger::LoggedJob

    def perform(args)
       # ...
    end
end

License

All code is under the MIT licence, see LICENSE.txt.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request