{"id":13463282,"url":"https://github.com/le0pard/mongodb_logger","last_synced_at":"2025-03-25T06:31:45.194Z","repository":{"id":1801654,"uuid":"2725653","full_name":"le0pard/mongodb_logger","owner":"le0pard","description":"MongoDB logger for Rails","archived":true,"fork":false,"pushed_at":"2016-12-15T09:56:47.000Z","size":2380,"stargazers_count":140,"open_issues_count":2,"forks_count":51,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-03-10T01:14:06.205Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://mongodb-logger.catware.org/","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"gitbucket/gitbucket","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/le0pard.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-11-07T11:25:23.000Z","updated_at":"2024-10-18T14:02:11.000Z","dependencies_parsed_at":"2022-08-20T22:31:07.766Z","dependency_job_id":null,"html_url":"https://github.com/le0pard/mongodb_logger","commit_stats":null,"previous_names":[],"tags_count":35,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/le0pard%2Fmongodb_logger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/le0pard%2Fmongodb_logger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/le0pard%2Fmongodb_logger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/le0pard%2Fmongodb_logger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/le0pard","download_url":"https://codeload.github.com/le0pard/mongodb_logger/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245414078,"owners_count":20611357,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T13:00:49.749Z","updated_at":"2025-03-25T06:31:44.427Z","avatar_url":"https://github.com/le0pard.png","language":"Ruby","readme":"# MongodbLogger\n\n[![Build Status](https://travis-ci.org/le0pard/mongodb_logger.svg?branch=master)](https://travis-ci.org/le0pard/mongodb_logger)\n[![Code Climate](https://codeclimate.com/github/le0pard/mongodb_logger/badges/gpa.svg)](https://codeclimate.com/github/le0pard/mongodb_logger)\n\nMongodbLogger is a alternative logger for Rails or Rack based app, which log all requests of you application into MongoDB database.\nIt:\n\n* simple to integrate into existing Rails application;\n* allow to store all logs from web cluster into one scalable storage - MongoDB;\n* flexible schema of MongoDB allow to store and search any information from logs;\n* web panel allow filter logs, build graphs using MapReduce by information from logs;\n\n## Rails support\n\nPlease note the latest version is compatible with rails 3.1.x or newer.\n\nFor rails 3.0.x latest version 0.2.8.\n\nDoesn't support the Rails version below 3.\n\n## Installation\n\n1. Add the following to your Gemfile then refresh your dependencies by executing \"bundle install\" (or just simple \"bundle\"):\n\n        gem \"mongodb_logger\"\n\n1. Add adapter in Gemfile. Supported mongo and moped (mongoid). For example:\n\n        gem \"mongo\"\n        gem \"bson_ext\"\n\n  or\n\n        gem \"moped\"\n\n1. Add the following line to your ApplicationController:\n\n        include MongodbLogger::Base\n\n1. For use with Heroku you need to prevent the rails\\_log\\_stdout plugin from being added by Heroku for rails 3:\n\n        mkdir vendor/plugins/rails_log_stdout\n        touch vendor/plugins/rails_log_stdout/.gitkeep\n\n   For Rails 4 just remove from Gemfile \"rails_12factor\" gem.\n\n1. Add MongodbLogger settings to database.yml for each environment in which you want to use the MongodbLogger. The MongodbLogger will also\n   look for a separate mongodb\\_logger.yml or mongoid.yml (if you are using mongoid) before looking in database.yml.\n   In the mongodb\\_logger.yml and mongoid.yml case, the settings should be defined without the 'mongodb\\_logger' subkey.\n\n   database.yml:\n\n        development:\n          adapter: postgresql\n          database: my_app_development\n          username: postgres\n          mongodb_logger:\n            database: my_app               # required (the only required setting)\n            capped: true                   # default: true  - warning: uncapped collections introduce the vulnerability that the size of the collection grows too high, exceeding all avaialble disk space\n            capsize: \u003c%= 10.megabytes %\u003e   # default: 250MB - ignored if capped is set to false\n            host: localhost                # default: localhost\n            port: 27017                    # default: 27017\n            username: null                 # default: null, username for MongoDB Auth\n            password: null                 # default: null, password for MongoDB Auth\n            replica_set: true              # default: false - Adds retries for ConnectionFailure during voting for replica set master\n            write_options:                 # default: {w: 0, wtimeout: 200} - write options for inserts (w - wait for insert to propagate to \"w\" numbers of nodes)\n              w: 0\n              wtimeout: 200\n            application_name: my_app       # default: Rails.application\n            disable_file_logging: false    # default: false - disable logging into filesystem (only in MongoDB)\n            collection: some_name          # default: Rails.env + \"_log\" - name of MongoDB collection\n\n   mongodb\\_logger.yml:\n\n        development:\n          database: my_app\n          capsize: \u003c%= 10.megabytes %\u003e\n          host: localhost\n          port: 27017\n          replica_set: true\n\n   Also you can use \"url\" parameter for setup connection to mongodb:\n\n        development:\n          url: mongodb://username:password@localhost:27017/my_app\n          capsize: \u003c%= 10.megabytes %\u003e\n\n\n1. For using with MongoDB Replica Set (more info you can read by this link [http://www.mongodb.org/display/DOCS/Replica+Sets](http://www.mongodb.org/display/DOCS/Replica+Sets)). In config set list of [host, port] in key \"hosts\":\n\n        development:\n          database: my_app\n          capsize: \u003c%= 10.megabytes %\u003e\n          host: localhost\n          port: 27017\n          hosts:\n            - - 127.0.0.1\n              - 27018\n            - - 127.0.0.1\n              - 27019\n\n1. For assets pipeline you can generate all js/css file into folder by rake task:\n\n        rake mongodb_logger:assets:compile[public/assets]\n\n## Assets pipeline\n\nFor capistrano possible compile assets by receipt. Add this to config/deploy.rb:\n\n    require 'mongodb_logger/capistrano'\n    set :mongodb_logger_assets_dir, \"public/assets\" # where to put mongodb assets\n    after 'deploy:update_code', 'mongodb_logger:precompile'\n\nAlso you can serve assets from rails app. You need just mount it separately:\n\n    mount MongodbLogger::Server.new, :at =\u003e \"/mongodb\", :as =\u003e :mongodb\n    mount MongodbLogger::Assets.instance, :at =\u003e \"/mongodb/assets\", :as =\u003e :mongodb_assets # assets\n\n\n## Usage\n\nAfter success instalation of gem, a new MongoDB document (record) will be created for each request on your application,\nby default will record the following information: Runtime, IP Address, Request Time, Controller, Method,\nAction, Params, Application Name and All messages sent to the logger. The structure of the MongoDB document looks like this:\n\n    {\n      'action'           : action_name,\n      'application_name' : application_name (rails root),\n      'controller'       : controller_name,\n      'ip'               : ip_address,\n      'messages'         : {\n                             'info'  : [ ],\n                             'debug' : [ ],\n                             'error' : [ ],\n                             'warn'  : [ ],\n                             'fatal' : [ ]\n                           },\n      'params'           : { },\n      'path'             : path,\n      'request_time'     : date_of_request,\n      'runtime'          : elapsed_execution_time_in_milliseconds,\n      'url'              : full_url,\n      'method'           : request method (GET, POST, OPTIONS),\n      'session'          : information from session,\n      'is_exception'     : true only for exceptions (in other cases this field miss)\n    }\n\nBeyond that, if you want to add extra information to the base of the document (let's say something like user\\_id on every request that it's available),\nyou can just call the Rails.logger.add\\_metadata method on your logger like so (for example from a before\\_filter):\n\n    # make sure we're using the MongodbLogger in this environment\n    Rails.logger.add_metadata(user_id: @current_user.id) if Rails.logger.respond_to?(:add_metadata)\n\n## Callback on exceptions\n\nFor send email or do something on exception you can add callback:\n\n    MongodbLogger::Base.configure do |config|\n      config.on_log_exception do |mongo_record|\n        # do something with this data, for example - send email (better - by background job)\n      end\n    end\n\nIn this callback send record without \"\\_id\", because logger not wait for insert response from MongoDB.\n\n## Disable mongodb_logger\n\nTo disable MongodbLogger you can use option disable. But this should be set before rails load (for example in Rails app the top of \"config/application.rb\"):\n\n    MongodbLogger::Base.configure do |config|\n      config.disable = true\n    end\n\nor\n\n     MongodbLogger::Base.disable = true\n\n## Migrate to another size of capped collection\n\nIf you need change capper collection size, you should change the \"capsize\" key in mongodb\\_config and run this task for migration:\n\n    rake mongodb_logger:migrate\n\n## Rack Middleware\n\nIf you want use MongodbLogger in Rack app which is mounted to your Rails app, you can try to use rack middleware:\n\n    use MongodbLogger::RackMiddleware\n\n## Rails::Engine\n\nIf you want use MongodbLogger with some of Rails::Engine, you can do this (example for Spree):\n\n    Spree::BaseController.send :include, MongodbLogger::Base\n    Spree::Admin::BaseController.send :include, MongodbLogger::Base # for admin\n\n## The Front End\n\nTo setup web interface in you Rails application, first of all create autoload file in you Rails application\n\nFile: you\\_rails\\_app/config/initializers/mongodb\\_logger.rb (example)\n\n    require 'mongodb_logger/server' # required\n    # this secure you web interface by basic auth, but you can skip this, if you no need this\n    MongodbLogger::Server.use Rack::Auth::Basic do |username, password|\n        [username, password] == ['admin', 'password']\n    end\n\nand just mount MongodbLogger::Server in rails routes:\n\nFile: you\\_rails\\_app/config/routes.rb\n\n    mount MongodbLogger::Server.new, :at =\u003e \"/mongodb\"\n\nNow you can see web interface by url \"http://localhost:3000/mongodb\"\n\nIf you've installed MongodbLogger as a gem and want running the front end without Rails application, you can do it by this command:\n\n    mongodb_logger_web config.yml\n\nwhere config.yml is config, similar to config of Rails apps, but without Rails.env. Example:\n\n    database: app_logs_dev\n    host: localhost\n    port: 27017\n    collection: development_log # set for see development logs\n\nparameter \"collection\" should be set, if your set custom for your Rails application or start this front end not for production\nenviroment (by default taken \"production\\_log\" collection, in Rails application gem generate \"#{Rails.env}\\_log\" collection,\nif it is not defined in config).\n\nIt's a thin layer around rackup so it's configurable as well:\n\n    mongodb_logger_web config.yml -p 8282\n\n###  Passenger, Unicorn, Thin, etc.\n\nUsing Passenger, Unicorn, Thin, etc? MongodbLogger ships with a `config.ru` you can use. See  guide:\n\n* Passenger Apache: \u003chttp://www.modrails.com/documentation/Users%20guide%20Apache.html#_deploying_a_rack_based_ruby_application\u003e\n* Passenger Nginx: \u003chttp://www.modrails.com/documentation/Users%20guide%20Nginx.html#deploying_a_rack_app\u003e\n* Unicorn: \u003chttp://unicorn.bogomips.org\u003e\n* Thin: \u003chttp://code.macournoyer.com/thin/usage\u003e\n\nDon't forget setup MONGODBLOGGERCONFIG env variable, which provide information about MongodbLogger config. Example starting with unicorn:\n\n    MONGODBLOGGERCONFIG=examples/server_config.yml unicorn\n\n##  Demo Application with MongodbLogger\n\nDemo: [http://demo-mongodb-logger.catware.org/](http://demo-mongodb-logger.catware.org/)\n\nDemo Sources: [https://github.com/le0pard/mongodb_logger_example_heroku](https://github.com/le0pard/mongodb_logger_example_heroku)\n\n\n## Querying via the Rails console\n\n  And now, for a couple quick examples on getting ahold of this log data...\n  First, here's how to get a handle on the MongoDB from within a Rails console:\n\n    \u003e\u003e db = Rails.logger.mongo_adapter.connection\n    =\u003e #\u003cMongo::DB:0x007fdc7c65adc8 @name=\"monkey_logs_dev\" ... \u003e\n    \u003e\u003e collection = Rails.logger.mongo_adapter.collection\n    =\u003e #\u003cMongo::Collection:0x007fdc7a4d12b0 @name=\"development_log\" .. \u003e\n\n  Once you've got the collection, you can find all requests for a specific user (with id):\n\n    \u003e\u003e cursor = collection.find(:user_id =\u003e '12355')\n    =\u003e #\u003cMongo::Cursor:0x1031a3e30 ... \u003e\n    \u003e\u003e cursor.count\n    =\u003e 5\n\n  Find all requests that took more that one second to complete:\n\n    \u003e\u003e collection.find({:runtime =\u003e {'$gt' =\u003e 1000}}).count\n    =\u003e 3\n\n  Find all order#show requests with a particular order id (id=order\\_id):\n\n    \u003e\u003e collection.find({\"controller\" =\u003e \"order\", \"action\"=\u003e \"show\", \"params.id\" =\u003e order_id})\n\n  Find all requests with an exception that contains \"RoutingError\" in the message or stack trace:\n\n    \u003e\u003e collection.find({\"messages.error\" =\u003e /RoutingError/})\n\n  Find all requests with errors:\n\n    \u003e\u003e collection.find({\"is_exception\" =\u003e true})\n\n  Find all requests with a request\\_date greater than '11/18/2010 22:59:52 GMT'\n\n    \u003e\u003e collection.find({:request_time =\u003e {'$gt' =\u003e Time.utc(2010, 11, 18, 22, 59, 52)}})\n\n## Using Uncapped Collections as Storage\n\nMongoDB's capped collections are a safe choice for storing logs where expiration happens automatically after exceeding the provided `capsize` (first in first out expiration).\n\nCapped collections comes with a few limitations, one of them being that you cannot manually delete log entries. Switching to a `capped: false` configuration will store all log entries in an uncapped collection and remove the constraints of uncapped collections.\n\n**Warning:** If you choose to deploy mongodb_logger with an uncapped collection configuration, you should implement an alternative way of cleaning up log records (cron job or similar). Uncapped collections can grow indifinely in size and take up more disk space than you anticipated.\n\nCopyright (c) 2009-2014 Phil Burrows, CustomInk (based on https://github.com/customink/central_logger) and Leopard released under the MIT license\n","funding_links":[],"categories":["Maintenance \u0026 Monitoring","Logging","Gems"],"sub_categories":["Logging"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fle0pard%2Fmongodb_logger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fle0pard%2Fmongodb_logger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fle0pard%2Fmongodb_logger/lists"}