Chloe
Chloe

Reputation: 26264

How do I make Rails log timestamps in UTC timezone?

How do I make Rails print the log timestamps in UTC instead of localtime? It currently prints

Started GET "/" for 190.176.185.42 at 2013-08-21 18:27:56 -0400

This will make it easier to find errors reported by users. I saw this http://apidock.com/rails/Logger and tried

production.rb

  Rails.logger.datetime_format = "%Y-%m-%d %H:%M:%S %Z"

but it gave an error

undefined method `datetime_format=' for nil:NilClass (NoMethodError)

I don't think just setting the format will work. I think I need to actually convert the time into UTC first.

Rails 3.2.14.

Upvotes: 3

Views: 2419

Answers (2)

Chloe
Chloe

Reputation: 26264

Yeah it worked! Using Yanhao's info, at the bottom of production.rb,

Rails::Rack::Logger.class_eval do
  # Override logging to spit out UTC time to easier find user reported errors
  # https://github.com/rails/rails/blob/v3.2.14/railties/lib/rails/rack/logger.rb#L38
  def started_request_message(request)
    'Started %s "%s" for %s at %s' % [
      request.request_method,
      request.filtered_path,
      request.ip,
      Time.now.utc ]
  end
end

Upvotes: 2

Yanhao
Yanhao

Reputation: 5294

That line of log is generated by started_request_message. It seems hard to change it because it is hard coded to use the default format. Maybe you have to override this method for your purpose.

Upvotes: 2

Related Questions