MongoRequestLogger
Log requests in a structured format to MongoDB.
Installation
Add this line to your application's Gemfile:
gem 'mongoid', '~> 3.0'
gem 'mongo_request_logger'
Usage with Rails
Add a logger
session to your Mongoid config, config/mongoid.yml
. An additional option
capsize
must be provided.
Example:
development:
sessions:
loggger:
database: your_log_db
hosts:
- host1:27017
- host2:27017
- host3:27017
options:
safe: false
consistency: eventual
capsize: 1000 # MB
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
session = Moped::Session.new(%w(127.0.0.1:27017))
session.use 'your_log_db'
session.[:capsize] = 10 # MB
MongoRequestLogger.configure(session: session)
# 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
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request