开发者

Jython logging gets time wrong

开发者 https://www.devze.com 2023-01-31 04:03 出处:网络
I have a Jython script that uses the Python logging module for logging. On one machine the script logs fine and on the other the time logged is shifted five hours into the future. Presumably this has

I have a Jython script that uses the Python logging module for logging. On one machine the script logs fine and on the other the time logged is shifted five hours into the future. Presumably this has to do with the machine being at GMT-5. What do you think can cause this issue?

Here's how I create the logging object:

log =开发者_如何学运维 logging.getLogger("my_log")
log.setLevel(logging.DEBUG)
handler = logging.handlers.RotatingFileHandler(
        log_file,
        maxBytes=log_size,
        backupCount=logs_count)
log_format = logging.Formatter("%(asctime)s:%(levelname)s:%(message)s")
handler.setFormatter(log_format)
handler.setLevel(logging.DEBUG)
log.addHandler(handler)

I did look at the log_format.converter function. I did make sure the time is set properly on the problematic machine. And I did make sure Python prints the correct time in the Python shell.

Edit: If I look at time.localtime() from the Python shell (2.4.3) then I get a tuple with correct local time values. If I look at time.localtime() from within the Jython script (2.5.2RC1) then I see the time in GMT.


I think I found the issue. Apparently Java itself was getting the time wrong, and the reason was that the timezone was not set properly for Java on the problematic machine. export TZ='US/East' in /etc/profile.d/java.sh seems to solve the issue.

This took me some time to figure out because running date from the shell output the correct local time and timezone information. Python too was able to figure out the timezone correctly.

Thanks guys!


I would check where you obtain your timestamp to make sure it is in UTC, and I would check where you hand over the timestamp to make sure that it is actually interpreted by the library as UTC.


What is log_format.converter set to? According to the documentation, it defaults to time.localtime. If you change it to time.gmtime, all times will use GMT.

0

精彩评论

暂无评论...
验证码 换一张
取 消