开发者

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


  1. Load data from files.
  2. Insert logs into mongodb collection(here detailed ruby driver tutolrial).
  3. 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.

Logging into a MongoDB collection (Ruby)

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜