Why does Heroku log using the server time rather than the Rails time zone?

后端 未结 3 1841
灰色年华
灰色年华 2020-12-12 17:23

UPDATE: Ok, I didn\'t formulate a good Q to be answered. I still struggle with heroku being on -07:00 UTC and I at +02:00 UTC.

Q: How do I get the log writte

相关标签:
3条回答
  • 2020-12-12 18:00

    I found that the above solutions did not work on Heroku. I put this in my config/environments/production.rb Make sure you put it before your logger is initialized

    Rails::Rack::Logger.class_eval do
    # Override logging to spit out time in different zone 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.in_time_zone('Melbourne') ]
     end
    end
    
    0 讨论(0)
  • 2020-12-12 18:09

    Without being able to change the actual server time (which I don't think you'll be able to do on Heroku), your only option is to convert the times yourself.

    If Time.zone.now is too cumbersome, you could set a global timezone using the tzinfo gem:

    $tz = TZInfo::Timezone.get("Europe/Oslo")
    $tz.now # current time in Norway
    

    But this is still a global change in your app and not significantly different from what you're doing.

    0 讨论(0)
  • 2020-12-12 18:10

    After some further investigation into my own Heroku timezone problems, I found a post which indicates that you actually can specify the timezone at an application level, using the following command:

    heroku config:add TZ=Europe/Oslo
    

    I believe this may be the answer to all your troubles. Courtesy of http://www.reality.hk/articles/2010/01/07/1319/ (Edit: Broken link as of 2012.08.23. Archived copy.)

    0 讨论(0)
提交回复
热议问题