Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

@fk_2000

td-agentをインストールしてRailsのログを見やすくする(後編)

前回
fluentdをインストールしてRailsのログを見やすくする(前編)

fluentdとtd-agentの違い

fluentdとtd-agentの違い

- td-agentはFluentdの安定版
- gem installするのはFluentd、RPMインストールするのはtd-agent
- gem installでもRPMインストールもfluent-catが同梱される

インストール手順

RPMインストール
curl -L https://toolbelt.treasuredata.com/sh/install-redhat-td-agent2.sh | sh

インストールバージョンの確認

$ td-agent --version
td-agent 0.12.29

実行ユーザの変更
$ vi /etc/td-agent/td-agent.conf

- TD_AGENT_USER=td-agent
- TD_AGENT_GROUP=td-agent
+ TD_AGENT_USER=root
+ TD_AGENT_GROUP=root

起動、リロード、停止

$sudo service td-agent start
$sudo service td-agent reload
$sudo service td-agent stop

fluent-catの使い方

fluent-catの場所

/opt/td-agent/embedded/bin/fluent-cat

環境変数に追加

export PATH=$PATH:/opt/td-agent/embedded/bin/

ログ出力テスト

echo '{"first":"hello"}' | fluent-cat debug.test

残りタスク

  • gem install fluent-plugin-rewrite
  • $ vi /vagrant/config/environments/development.rb設定変更
    • config.logger = Logger.new(config.paths["log"].first)
    • config.logger.formatter = Logger::Formatter.new
  • $ vi /vagrant/config/environment.rb設定変更(*1)
  • /etc/td-agent/td-agent.conf設定変更(<source><match>追加)
  • ログ確認

*1

class Logger
  class Formatter
    def call(severity, time, progname, msg)
      if msg.class.to_s == "String"
        msg = msg.gsub(/\n/, "")
        if msg.present? && !msg.include?("assets") && !msg.include?("erb")
          format = "[%s %d] %5s -- %s: \'%s\'\n"
          format % ["#{time.strftime('%Y-%m-%d %H:%M:%S')}.#{'%06d' % time.usec.to_s}",$$, severity, progname, msg2str(msg)]
        end
      end
    end
  end
end 
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
1
Help us understand the problem. What are the problem?