LoginSignup
14
14

More than 5 years have passed since last update.

Mac OS X で Fluentd を使って MongoDB にログ出力してみる

Last updated at Posted at 2012-11-28

前回記事

Mac OS X に Fluentd をインストールして Ruby からログ出力してみる

前提

MongoDB をインストールしておいてください。ここでは Homebrew で入れたものを使って行っています。

MongoDB プラグインのインストール

gem で fluent-plugin-mongo をインストールします。

$ gem install fluent-plugin-mongo

このまま Fluentd を起動すると、

**Notice: C extension not loaded. This is required for optimum MongoDB Ruby driver performance.
  You can install the extension as follows:
  gem install bson_ext

If you continue to receive this message after installing, make sure that the bson_ext gem is in your load path and that the bson_ext and mongo gems are of the same version.

と言われたので、mongo と同じバージョンの bson_ext もインストールしました。

$ gem install bson_ext

Fluentd の設定

デフォルトの設定の debug.** の部分を以下のように変えて、標準出力と MongoDB の両方にログを飛ばすようにしてみました。

<match debug.**>
  type copy
  <store>
    type stdout
  </store>
  <store>
    type mongo
    host localhost
    database fluent
    collection debug
  </store>
</match>

MongoDB にログ出力してみる

上記設定で Fluentd を起動します。

$ fluentd -c ./fluent/fluent.conf
2012-11-28 14:53:10 +0900: starting fluentd-0.10.29
2012-11-28 14:53:10 +0900: reading config file path="./fluent/fluent.conf"
2012-11-28 14:53:10 +0900: adding source type="forward"
2012-11-28 14:53:10 +0900: adding source type="http"
2012-11-28 14:53:10 +0900: adding source type="debug_agent"
2012-11-28 14:53:10 +0900: adding match pattern="debug.**" type="copy"
2012-11-28 14:53:10 +0900: listening fluent socket on 0.0.0.0:24224
2012-11-28 14:53:10 +0900: listening dRuby uri="druby://0.0.0.0:24230" object="Engine"

すると、ローカルの MongoDB に fluent データベース、debug コレクションが自動で作られました。

そして、

$ echo '{"hoge":"fuga"}' | fluent-cat debug.test

などと出力すると、標準出力に

2012-11-28 14:58:12 +0900 debug.test: {"hoge":"fuga"}

と表示され、MongoDB の方にも、

$ mongo fluent
MongoDB shell version: 2.2.1
connecting to: fluent
> db.debug.find()
{ "_id" : ObjectId("50b5a7f7e0395a29bd000001"), "hoge" : "fuga", "time" : ISODate("2012-11-28T05:58:12Z") }

という形で格納されました。

14
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
14