前提
- ruby: 2.7.1
- Rails: 6.0.3
自分好みの Formatter を定義する
config/logger_my_formatter.rb
class Logger
class MyFormatter < Formatter
def call(severity, time, progname, msg)
"[%s#%d] %5s -- %s: %s\n" % [format_datetime(time), $$, severity, progname, msg]
end
end
end
自分好みの Formatter を指定する
config/application.rb
require_relative 'boot'
require_relative 'logger_my_formatter' # 追記
# ...
動作確認は production
モードで行います(development
モードはActiveSupport::Logger::SimpleFormatter
)。
config/environments/production.rb
Rails.application.configure do
# ...
config.log_formatter = ::Logger::MyFormatter.new # 追記
end
動作確認
デフォルト
I, [2020-06-19T16:16:21.592424 #4688] INFO -- : [e8b5388b-e8d9-4a31-a350-1f3cafdd0dda] Completed 200 OK in 224ms (Views: 102.2ms | ActiveRecord: 40.3ms | Allocations: 65613)
変更後
[2020-06-19T16:21:53.497691 #5177] INFO -- : [d8ef0edd-4d3b-4618-bba7-050a185fa30b] Completed 200 OK in 12ms (Views: 6.3ms | ActiveRecord: 2.5ms | Allocations: 5132)