&tag(Rails/ログ出力);
#サイズで分割(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つの要素しか格納されていない模様。
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)
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)
config.logger.formatter = MyFormatter.new
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 #これが正解
127.0.0.1 - - [23/Jan/2019:22:16:58 JST] "GET /books HTTP/1.1" 200 1993 - -> /books
# 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