Home > Net >  With Rails 4.2 and lograge, how do I enable date/times before each logged line?
With Rails 4.2 and lograge, how do I enable date/times before each logged line?

Time:11-18

I use the gem “Lograge” 0.3.6, and Rails 4.2. I have this configured in my config/environments/development.rb file

  config.lograge.enabled = true
  config.lograge.formatter = CustomLogstash.new

However, I notice the output in my log/development.log file doesn’t contain date/times in front of each line. How do I configure lograge (or maybe just my Rails logger?) to prefix each line in that file with a date and time?

CodePudding user response:

Can you add the following in config if it helps.

config.lograge.formatter = ->(data) { data.reverse_merge({time: Time.now}) }# data is a ruby hash.

It will give you output like following

{:time=>2021-11-16 12:26:24.65362  0000, :method=>"GET", :path=>"/", :format=>:html, :controller=>"Controller", :action=>"index", :status=>200, :duration=>393.41, :view=>85.55, :db=>1.38}

CodePudding user response:

As per official documentation for lograge, you can make use of custom_options


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

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

You can use time:Time.now or time: event.time

  • Related