Logging into a MongoDB collection (Ruby)
I'm using MongoDB wit开发者_JS百科h a Ruby (sinatra) applicaion. Currently, all logging is done to local files. I would instead like to place log messages into a MongoDB collection. What is the best way to do this?
There's an open source project that does just that:
https://github.com/customink/central_logger
- Load data from files.
- Insert logs into mongodb collection(here detailed ruby driver tutolrial).
- In method that log data instead of writing in the file just insert logs in mongodb collection.
Also capped collections good fit for such tasks as logging.
When you use MongoDB for logging, the concern is high write throughput. Although MongoDB's insert is fire-and-forget style by default, calling a lot of insert() causes a heavy write lock contention. This could affect the application performance, and prevent the readers to aggregate / filter the stored logs.
One solution might be using the log collector framework such as Fluentd, Logstash, or Flume. These daemons are supposed to be launched at every application nodes, and takes the logs from app processes.
They buffer the logs and asynchronously writes out the data to other systems like MongoDB / PostgreSQL / etc. The write is done by batches, so it's a lot more efficient than writing directly from apps. This link describes how to put the logs into Fluentd from Ruby program.
- Fluentd: Data Import from Ruby Applications
- Fluentd + MongoDB: The Easiest Way to Log Your Data Effectively on 10gen blog
精彩评论