Python Twisted logger能否登录UTC?
如果我使用Python Twisted logger能否登录UTC?,python,logging,twisted,Python,Logging,Twisted,如果我使用日志记录,我可以通过设置 logging.Formatter.converter = time.gmtime 像这样: import sys import time import logging handler = logging.StreamHandler(sys.stdout) out_fmt = '[%(asctime)s.%(msecs)03dZ] [%(levelname)s] %(message)s' dt_fmt = '%Y-%m-%d %H:%M:%S' loggi
日志记录,我可以通过设置
logging.Formatter.converter = time.gmtime
像这样:
import sys
import time
import logging
handler = logging.StreamHandler(sys.stdout)
out_fmt = '[%(asctime)s.%(msecs)03dZ] [%(levelname)s] %(message)s'
dt_fmt = '%Y-%m-%d %H:%M:%S'
logging.Formatter.converter = time.gmtime
formatter = logging.Formatter(out_fmt, dt_fmt)
handler.setFormatter(formatter)
root = logging.getLogger()
root.setLevel(10)
root.addHandler(handler)
logging.log(logging.INFO, 'Start logging')
因为我的应用程序将使用Twisted,所以我想使用Twisted.logger
来代替日志记录。事实上,这要简单得多:
import sys
from twisted.logger import Logger, textFileLogObserver
log = Logger(observer=textFileLogObserver(sys.stdout, '%Y-%m-%d %H:%M:%S.%fZ'))
log.info('Start logging')
问题是,时间戳是本地时间,而不是UTC时间。有没有办法让Twisted logger使用UTC时间?我最终在FileLogObserver
中覆盖了formatTime
:
导入系统
从日期时间导入日期时间
从twisted.python导入日志
def myFLOformatTime(自我,何时):
timeFormatString=self.timeFormat
如果timeFormatString为无:
timeFormatString='[%Y-%m-%d%H:%m:%S.%fZ]'
return datetime.utcfromtimestamp(when).strftime(timeFormatString)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
log.FileLogObserver.formatTime=myFLOformatTime
日志记录(sys.stdout)
我最终覆盖了FileLogObserver
中的formatTime
:
导入系统
从日期时间导入日期时间
从twisted.python导入日志
def myFLOformatTime(自我,何时):
timeFormatString=self.timeFormat
如果timeFormatString为无:
timeFormatString='[%Y-%m-%d%H:%m:%S.%fZ]'
return datetime.utcfromtimestamp(when).strftime(timeFormatString)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
log.FileLogObserver.formatTime=myFLOformatTime
日志记录(sys.stdout)