Why does Heroku log using the server time rather t

2019-01-21 05:08发布

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 written in the correct Time.zone ? The 9 hours difference, heroku (us west) - norway, is distracting to work with. I get this in my production.log (using heroku logs):

Processing ProductionController#create to xml (for 81.26.51.35 at 2010-04-28 23:00:12) [POST]

How do I get it to write 2010-04-29 08:00:12 +02:00 GMT ?

Note that I'm running at heroku and cannot set the server time myself, as one could do at your amazon EC2 servers. Below is my previous question, I'll leave it be as it holds some interesting information about time and zones.


Why does Time.now yield the server local time when I have set the another time zone in my environment.rb

config.time_zone = 'Copenhagen'

I've put this in a view

<p> Time.zone <%= Time.zone %> </p>
<p> Time.now <%= Time.now %> </p>
<p> Time.now.utc <%= Time.now.utc %> </p>
<p> Time.zone.now <%= Time.zone.now %>  </p>
<p> Time.zone.today <%= Time.zone.today %> </p>

rendering this result on my app at heroku

Time.zone (GMT+01:00) Copenhagen

Time.now Mon Apr 26 08:28:21 -0700 2010

Time.now.utc Mon Apr 26 15:28:21 UTC 2010

Time.zone.now 2010-04-26 17:28:21 +0200

Time.zone.today 2010-04-26

Time.zone.now yields the correct result. Do I have to switch from Time.now to Time.zone.now, everywhere? Seems cumbersome. I truly don't care what the local time of the server is, it's giving me loads of trouble due to extensive use of Time.now. Am I misunderstanding anything fundamental here?

3条回答
Melony?
2楼-- · 2019-01-21 05:28

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

查看更多
劫难
3楼-- · 2019-01-21 05:31

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
查看更多
叼着烟拽天下
4楼-- · 2019-01-21 05:39

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.

查看更多
登录 后发表回答