2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Rails]logフォーマットを自分好みに変更する

Last updated at Posted at 2020-06-19

前提

  • 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)

:arrow_down:

変更後

[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)
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?