© 2023 Feiniao, Inc. All rights reserved.

如何在Rails中简化日志显示 - 怎么在Rails中简化日志显示?

Rails
Rails默认的日志输出很详细,就是不精简,我们该如何优化呢?
1, Gemfile中添加lograge

gem "lograge"

2, 在初始化器或相关环境配置中启用它。

# config/initializers/lograge.rb
# OR
# config/environments/production.rb
Rails.application.configure do
  config.lograge.enabled = true
end

3, 你可以添加自定义的一些内容。

Rails.application.configure do
  config.lograge.enabled = true

  config.lograge.custom_options = lambda do |event|
    { time: Time.now }
  end
end

简化之前,

D, [2020-02-02T13:00:37.114099 #98366] DEBUG -- :   ↳ app/views/layouts/_header.html.erb:45
D, [2020-02-02T13:00:37.114585 #98366] DEBUG -- :   Rendered layouts/_header.html.erb (Duration: 23.9ms | Allocations: 31943)
D, [2020-02-02T13:00:37.116442 #98366] DEBUG -- :   Rendered layouts/_footer.html.erb (Duration: 0.8ms | Allocations: 115)
D, [2020-02-02T13:00:37.116528 #98366] DEBUG -- :   Rendered layouts/_left_menu.html.erb (Duration: 1.7ms | Allocations: 1360)
I, [2020-02-02T13:00:37.116588 #98366]  INFO -- :   Rendered layout layouts/application.html.erb (Duration: 87.8ms | Allocations: 89070)
I, [2020-02-02T13:00:37.116767 #98366]  INFO -- : Completed 200 OK in 98ms (Views: 88.1ms | ActiveRecord: 1.2ms | Allocations: 98179)

简化之后,

D, [2020-02-02T12:59:27.641802 #94559] DEBUG -- :   ↳ app/views/layouts/_header.html.erb:45
I, [2020-02-02T12:59:27.642693 #94559]  INFO -- : method=GET path=/ format=html controller=HomeController action=index status=200 duration=10.17 view=9.12 db=0.24 time=2020-02-02 12:59:27 +0800