📜 ⬆️ ⬇️

MongodbLogger - add logs of your Rails 3 application to MongoDB

image

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:



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:

image

image

image

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.org
Sources: github.com/le0pard/mongodb_logger

Use and join :)

Source: https://habr.com/ru/post/134327/


All Articles