Help us understand the problem. What is going on with this article?

RailsログをCloud Watch Logsに流す

More than 3 years have passed since last update.

RailsログをCloudWatchLogに流し込みます。
全部流すとデカすぎるので、WARN以上を流しましょう。

OS: amazon-linux-ami 2016.09

fluent = td-agent(?)のインストール

curl -L http://toolbelt.treasuredata.com/sh/install-redhat-td-agent2.sh | sudo sh

fluent-plugin-cloudwatch-logsのインストール

fluentのcloud watch log用プラグイン
td-agent-gemにインストールする、要注意

sudo td-agent-gem install fluent-plugin-cloudwatch-logs

CloudWatch用のIAMを作って、アクセス情報を記載するよ

IAMのポリシー情報

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "logs:*",
        "s3:GetObject"
      ],
      "Resource": [
        "arn:aws:logs:us-east-1:*:*",
        "arn:aws:s3:::*"
      ]
    }
  ]
}

td-agentに記載

/etc/init.d/td-agent
#これをTD_AGENT_OPTIONSの下あたりに追記
export AWS_REGION="ap-northeast-1"
export AWS_ACCESS_KEY_ID="your access key id"
export AWS_SECRET_ACCESS_KEY="your secret access key"

railsログ情報を設定するよ

/etc/td-agent/td-agent.conf
<source>
      type tail
      path /path/to/rails_app/log/production.log
      pos_file /var/log/td-agent/logfile.log.pos
      tag app.error
      format /^(?<level>.), (?<log>.*)$/
</source>
<filter app.error>
  #WARN / ERROR / FATALだけ流すよ
  type grep
  regexp1 level [WEF]
</filter>
<match app.error>
      type cloudwatch_logs
      log_group_name your_log_group_name
      log_stream_name your_log_stream_name 
      auto_create_stream true
</match>

起動するよ

$ sudo service td-agent start
td-agentng td-agent:                                       [  OK  ]

これで完了、cloudWatchLogに流し込まれているはず。

fluentdの動作ログは /var/log/td-agent/td-agent.log に書かれているよ

TakenoriHirao
WEB Java / C#を経て、最近の好みはRuby / Scala / Kotlin / Swift、最近は結局Ruby
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away