7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RailsログをCloud Watch Logsに流す

Posted at

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 に書かれているよ

7
7
1

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
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?