#author("2019-01-23T22:28:18+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]

**ログフォーマットのカスタマイズ [#c24223aa]

-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 #これが正解
}}

**不要なログ出力を消す [#j334920a]
-以下のようなログがdevelopment環境で表示される。
#pre{{
127.0.0.1 - - [23/Jan/2019:22:16:58 JST] "GET /books HTTP/1.1" 200 1993
- -> /books
}}
-Railsが出力しているものではなくWEBRickが出力しているログらしい。[[Turning off WEBRick server logging (XMLRPC) - Ruby - Ruby-Forum:https://www.ruby-forum.com/t/turning-off-webrick-server-logging-xmlrpc/73141/6]]
--
-Pumaを使用するか(Gemfileにpumaを追加する)、以下のような設定で消せる?
#pre{{
# config/boot.rb
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)

require 'bundler/setup' # Set up gems listed in the Gemfile.

require 'rails/command'
require 'rails/commands/server/server_command'

# No stdout for logger
module Rails
  class Server < ::Rack::Server
    alias_method :orig_initialize, :initialize
    def initialize(options)
#      orig_initialize(options.merge(log_stdout: false, AccessLog: []))
     # falseにすると完全に消えるのでtrueにするとなぜか不要なログだけ削除される…?
      orig_initialize(options.merge(log_stdout: true, AccessLog: []))
    end
  end
end 
}}

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS