- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2019-01-23T14:33:33+09:00","default:wikiwriter","wikiwriter")
#author("2019-01-23T17:56:10+09:00","default:wikiwriter","wikiwriter")
&tag(Rails/ログ出力);
*目次 [#k298fad3]
#contents
*関連ページ [#d631d61e]
*参考情報 [#p4b49ac9]
*ログローテーションする [#i6be3a41]
-外部コマンドの「logrotate」を使用する方法もある。
-production.rbを編集場合以下のように変更する。[[Railsのログローテーション - Qiita:https://qiita.com/lumbermill/items/e05357577d16d702c16c]]
#サイズで分割(10MBごと)
config.logger = Logger.new("log/production.log", 5, 10 * 1024 * 1024)
config.logger = Logger.new(config.paths["log"].first, 5, 10.megabytes)
#日別
config.logger = Logger.new("log/production.log", 'daily')
ちなみにconfig.paths["log"].firstは"log/production.rb"が格納されている。pathsは複数のパスを管理することができるPathクラスのオブジェクトだがここでは1つの要素しか格納されていない模様。
*ログ出力のカスタマイズ [#da6c4e18]
-development環境の標準のログ。
#pre{{
Started GET "/books" for 127.0.0.1 at 2019-01-23 17:50:05 +0900
(0.5ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Processing by BooksController#index as HTML
Rendering books/index.html.erb within layouts/application
Book Load (0.2ms) SELECT "books".* FROM "books"
Rendered books/index.html.erb within layouts/application (3.8ms)
Completed 200 OK in 291ms (Views: 276.9ms | ActiveRecord: 0.5ms)
}}
-以下のようにLoggerを設定するだけで以下のようになる。
#pre{{
I, [2019-01-23T17:52:25.168643 #90293] INFO -- : Started GET "/books" for 127.0.0.1 at 2019-01-23 17:52:25 +0900
I, [2019-01-23T17:52:25.176907 #90293] INFO -- : Processing by BooksController#index as HTML
I, [2019-01-23T17:52:25.194435 #90293] INFO -- : Rendering books/index.html.erb within layouts/application
D, [2019-01-23T17:52:25.197667 #90293] DEBUG -- : Book Load (0.5ms) SELECT "books".* FROM "books"
I, [2019-01-23T17:52:25.199449 #90293] INFO -- : Rendered books/index.html.erb within layouts/application (4.8ms)
I, [2019-01-23T17:52:25.498990 #90293] INFO -- : Completed 200 OK in 322ms (Views: 311.0ms | ActiveRecord: 1.3ms)
}}
-Loggerを設定した場合「config.log_formatter」ではなく「config.logger.formatter」に設定する。
config.logger.formatter = MyFormatter.new
-formatterは以下のようにcallを定義するだけのクラスでよい。procでも可。
#pre{{
class LoggerFormatWithTime
def call(severity, timestamp, progname, msg)
"[#{timestamp.strftime("%Y/%m/%d %H:%M:%S" )}] [#{severity}] : #{String === msg ? msg : msg.inspect}\n"
end
end
config.logger = Logger.new(config.paths["log"].first, 5, 10 * 1024 * 1024)
# config.log_formatter = LoggerFormatWithTime.new # これは無意味
config.logger.formatter = LoggerFormatWithTime.new #これが正解
}}