
Hello to all. Today I will talk about my development for Rails 3 - MongodbLogger. Let's start in order.
Rails application by default puts the logs of requests in the logs folder. The logs themselves are a very handy thing - they help you see which requests go into your application, see the trace errors, and so on. You can add additional information in them. Although the file log is simple and effective, it has certain limitations: when using multiple web servers, each has its own log; no easy access. Writing to the RDBMS solves the issues of centralization and simple access of the logs, but other problems immediately arise: the table schema is not as flexible as the log structure can be; recording may not be fast enough; cleaning of old logs - tasks on your shoulders. And here comes MongoDB.
')
MongoDB (http://www.mongodb.org/) is a document-based open source database management system that does not require a description of the table schema. What are the benefits of MongoDB for logging:
- Flexible document collection;
- Asynchronous data insertion;
- Capped collection (self-cleaning collections - www.mongodb.org/display/DOCS/Capped+Collections );
- Quite fast recording in comparison with files;
- Indexing and searching;
- Data analysis (MapReduce);
To use MongoDB as a logger in Rails 3, I created a special gem, MongodbLogger. Installation is very simple:
Add to Gemfile
gem "mongodb_logger"
Add to ApplicationController
include MongodbLogger :: Base
we create a config for MongoDB; (examples in
github.com/le0pard/mongodb_logger/blob/master/README.md and more in the wiki -
github.com/le0pard/mongodb_logger/wiki/Mongodblogger-settings )
We connect the web (optional), add to config / routes.rb
require 'mongodb_logger / server'
mount MongodbLogger :: Server.new,: at => "/ mongodb"
All is ready. Now your Rails application will log requests to MongoDB.
Clickable screenshots of the web for MongodbLogger:



You can also search through the Rails console.
Now the logs will not exceed the specified size (the old records will be cleared automatically), filter only errors.
Convenience also lies in the fact that you can search by parameters in the query. For example, data from the form params [: order_id] = 12 comes from the form. You can search for such data by the log:
collection.find ({"controller" => "order", "action" => "show", "params.order_id" => order_id})
You can also almost create your own Google Analytic or
Airbrake , but this is not a task for this gem :) Although extensions can do it.
Official site:
mongodb-logger.catware.orgSources:
github.com/le0pard/mongodb_loggerUse and join :)