Railsでログを出力してみる

RailsでHello, world!を表示してみるの続き

Railsでログを出力してみる。

ログファイル

とりあえずログファイルを見てみる。
特にログ出力の処理がなくても、デフォルトで、アクセスのログがdevelopment.logに出力されている。

$ cat log/development.log

Started GET "/" for 192.168.56.1 at 2014-01-02 17:05:37 +0900
Processing by SampleController#index as HTML
  Rendered sample/index.html.erb within layouts/application (1.5ms)
Completed 200 OK in 92ms (Views: 66.1ms | ActiveRecord: 0.0ms)


Started GET "/assets/sample.css?body=1" for 192.168.56.1 at 2014-01-02 17:05:37 +0900


Started GET "/assets/jquery.js?body=1" for 192.168.56.1 at 2014-01-02 17:05:37 +0900


Started GET "/assets/jquery_ujs.js?body=1" for 192.168.56.1 at 2014-01-02 17:05:37 +0900


Started GET "/assets/turbolinks.js?body=1" for 192.168.56.1 at 2014-01-02 17:05:37 +0900


Started GET "/assets/sample.js?body=1" for 192.168.56.1 at 2014-01-02 17:05:37 +0900


Started GET "/assets/application.css?body=1" for 192.168.56.1 at 2014-01-02 17:05:37 +0900


Started GET "/assets/application.js?body=1" for 192.168.56.1 at 2014-01-02 17:05:37 +0900

loggerでログ出力

loggerでログを出力してみる。

indexにログの出力処理を追加し、

$ vi app/controllers/sample_controller.rb
class SampleController < ApplicationController
  def index
    logger.debug("Hello, world!")
  end
end

indexにアクセスすると、下記のようにログにHello, world!が出力される。

tail -f log/development.log

Started GET "/" for 192.168.56.1 at 2014-01-02 17:10:29 +0900
Processing by SampleController#index as HTML
Hello, world!
  Rendered sample/index.html.erb within layouts/application (0.1ms)
Completed 200 OK in 8ms (Views: 6.2ms | ActiveRecord: 0.0ms)

配列やハッシュもそのまま渡せばログに出力される。
オブジェクトはそのまま渡すと
#
のような文字列しか出力されないが、
「.inspect」を付ければ、オブジェクトの中身がログに出力される。

$ vi app/controllers/sample_controller.rb
class SampleController < ApplicationController
  def index
    logger.debug(["aaa", "bbb", "ccc"])
    logger.debug({:a => 1, :b => 2, :c => 3})
    logger.debug("--- inspectの指定 無し ---")
    logger.debug(logger)
    logger.debug("--- inspectの指定 有り ---")
    logger.debug(logger.inspect)
  end
end
$ tail -f log/development.log

Started GET "/" for 192.168.56.1 at 2014-01-02 17:19:51 +0900
Processing by SampleController#index as HTML
["aaa", "bbb", "ccc"]
{:a=>1, :b=>2, :c=>3}
--- inspectの指定 無し ---
#<ActiveSupport::Logger:0x007f10d6380660>
--- inspectの指定 有り ---
#<ActiveSupport::Logger:0x007f10d6380660 @progname=nil, @level=0, @default_formatter= ・・・中身の詳細 ・・・ @mon_count=0, @mon_mutex=#<Mutex:0x007f10d6380250>>>>
  Rendered sample/index.html.erb within layouts/application (0.1ms)
Completed 200 OK in 8ms (Views: 5.6ms | ActiveRecord: 0.0ms)

ログのレベル

loggerには下記の6種類のログレベルがある。

$ vi app/controllers/sample_controller.rb
class SampleController < ApplicationController
  def index
    logger.unknown("不明なエラー")
    logger.fatal("致命的なエラー")
    logger.error("エラー")
    logger.warn("警告")
    logger.info("通知")
    logger.debug("デバッグ情報")
  end
end
$ tail -f log/development.log

Started GET "/" for 192.168.56.1 at 2014-01-02 17:27:40 +0900
Processing by SampleController#index as HTML
不明なエラー
致命的なエラー
エラー
警告
通知
デバッグ情報
  Rendered sample/index.html.erb within layouts/application (0.5ms)
Completed 200 OK in 44ms (Views: 42.4ms | ActiveRecord: 0.0ms)
logger - リファレンス - Railsドキュメント
http://railsdoc.com/references/logger

development.rbにlog_levelを指定すると、指定されたレベル以上のログしか出力されなくなる。

$ vi config/environments/development.rb
config.log_level = :error	←追加
$ tail -f log/development.log
不明なエラー
致命的なエラー
エラー

ログのローテーション

development.rbに下記を追記すると、
10MBごとに5ファイルまで作成するようになる。

config.logger = ActiveSupport::Logger.new("log/development.log", 5, 10.megabytes)

development.rbに下記を追記すると、
日毎にローテートするようになる。

config.logger = ActiveSupport::Logger.new("log/development.log", "daily")
下記のようにconfig.log_pathを指定すると、
config.logger = ActiveSupport::Logger.new(config.log_path, "daily")
なぜか
undefined method `log_path'
とエラーになる。
development.rbを修正した際、apacheを再起動しないと反映されない。
なんで?

<2014/6/22追記>
頂いたコメントによると、development.rbはRails起動時に
読み込まれる部分のため、再起動が必要になるそうです。
akn_epさん、情報ありがとうございます。

Logger

loggerにActiveSupportではないLoggerを指定すると、

config.logger = Logger.new("log/development.log")

ログの形式は下記のような感じになる。

A, [2014-01-02T19:03:49.921374 #5459]   ANY -- : 不明なエラー
F, [2014-01-02T19:03:49.921597 #5459] FATAL -- : 致命的なエラー
E, [2014-01-02T19:03:49.921789 #5459] ERROR -- : エラー

以下のサイトによると、ActiveSupport::LoggerはLoggerの拡張版みたい。

RailsDoc - ActiveSupport::Logger
http://railsdoc.eiel.info/active_support/logger/

よくわからん。

ログの削除

溜まったログは、log:clearで削除できる。

rake log:clear