问题:Python日志记录:使用毫秒格式的时间
默认情况下logging.Formatter('%(asctime)s'),使用以下格式打印:
2011-06-09 10:54:40,638其中638是毫秒。我需要将逗号更改为点:
2011-06-09 10:54:40.638要格式化时间,我可以使用:
logging.Formatter(fmt='%(asctime)s',datestr=date_format_str)但是,文档未指定如何设置毫秒格式。我发现这太问题,其中约微秒的会谈,但)我宁愿毫秒和b)下列不上的Python 2.6(其中我的工作),由于工作的关系%f:
logging.Formatter(fmt='%(asctime)s',datefmt='%Y-%m-%d,%H:%M:%S.%f')回答 0
请注意,克雷格·麦克丹尼尔(Craig McDaniel)的解决方案显然更好。
logging.Formatter的formatTime方法如下所示:
def formatTime(self, record, datefmt=None):
    ct = self.converter(record.created)
    if datefmt:
        s = time.strftime(datefmt, ct)
    else:
        t = time.strftime("%Y-%m-%d %H:%M:%S", ct)
        s = "%s,%03d" % (t, record.msecs)
    return s请注意中的逗号"%s,%03d"。不能通过指定a来解决此问题,datefmt因为cta是,time.struct_time并且这些对象不记录毫秒。
如果我们更改的定义ct以使其成为datetime对象而不是struct_time,那么(至少在现代版本的Python中)可以调用ct.strftime,然后可以用来%f设置微秒的格式:
import logging
import datetime as dt
class MyFormatter(logging.Formatter):
    converter=dt.datetime.fromtimestamp
    def formatTime(self, record, datefmt=None):
        ct = self.converter(record.created)
        if datefmt:
            s = ct.strftime(datefmt)
        else:
            t = ct.strftime("%Y-%m-%d %H:%M:%S")
            s = "%s,%03d" % (t, record.msecs)
        return s
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
console = logging.StreamHandler()
logger.addHandler(console)
formatter = MyFormatter(fmt='%(asctime)s %(message)s',datefmt='%Y-%m-%d,%H:%M:%S.%f')
console.setFormatter(formatter)
logger.debug('Jackdaws love my big sphinx of quartz.')
# 2011-06-09,07:12:36.553554 Jackdaws love my big sphinx of quartz.或者,要获取毫秒数,请将逗号更改为小数点,然后省略datefmt参数:
class MyFormatter(logging.Formatter):
    converter=dt.datetime.fromtimestamp
    def formatTime(self, record, datefmt=None):
        ct = self.converter(record.created)
        if datefmt:
            s = ct.strftime(datefmt)
        else:
            t = ct.strftime("%Y-%m-%d %H:%M:%S")
            s = "%s.%03d" % (t, record.msecs)
        return s
...
formatter = MyFormatter(fmt='%(asctime)s %(message)s')
...
logger.debug('Jackdaws love my big sphinx of quartz.')
# 2011-06-09 08:14:38.343 Jackdaws love my big sphinx of quartz.回答 1
这也应该工作:
logging.Formatter(fmt='%(asctime)s.%(msecs)03d',datefmt='%Y-%m-%d,%H:%M:%S')回答 2
添加毫秒是更好的选择,谢谢。这是我在Blender中将其与Python 3.5.3结合使用的修正
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s.%(msecs)03d %(levelname)s:\t%(message)s', datefmt='%Y-%m-%d %H:%M:%S')
log = logging.getLogger(__name__)
log.info("Logging Info")
log.debug("Logging Debug")回答 3
我发现的最简单的方法是覆盖default_msec_format:
formatter = logging.Formatter('%(asctime)s')
formatter.default_msec_format = '%s.%03d'回答 4
实例化后,Formatter我通常会设置formatter.converter = gmtime。因此,在这种情况下,为了使@unutbu的答案起作用,您需要:
class MyFormatter(logging.Formatter):
    def formatTime(self, record, datefmt=None):
        ct = self.converter(record.created)
        if datefmt:
            s = time.strftime(datefmt, ct)
        else:
            t = time.strftime("%Y-%m-%d %H:%M:%S", ct)
            s = "%s.%03d" % (t, record.msecs)
        return s回答 5
一个不需要datetime模块且不受其他解决方案限制的简单扩展就是使用简单的字符串替换,如下所示:
import logging
import time
class MyFormatter(logging.Formatter):
    def formatTime(self, record, datefmt=None):
    ct = self.converter(record.created)
    if datefmt:
        if "%F" in datefmt:
            msec = "%03d" % record.msecs
            datefmt = datefmt.replace("%F", msec)
        s = time.strftime(datefmt, ct)
    else:
        t = time.strftime("%Y-%m-%d %H:%M:%S", ct)
        s = "%s,%03d" % (t, record.msecs)
    return s这样,可以使用%F毫秒来编写所需的日期格式,甚至允许区域差异。例如:
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
sh = logging.StreamHandler()
log.addHandler(sh)
fm = MyFormatter(fmt='%(asctime)s-%(levelname)s-%(message)s',datefmt='%H:%M:%S.%F')
sh.setFormatter(fm)
log.info("Foo, Bar, Baz")
# 03:26:33.757-INFO-Foo, Bar, Baz回答 6
如果您使用箭头,或者您不介意使用箭头。您可以将python的时间格式替换为arrow的时间格式。
import logging
from arrow.arrow import Arrow
class ArrowTimeFormatter(logging.Formatter):
    def formatTime(self, record, datefmt=None):
        arrow_time = Arrow.fromtimestamp(record.created)
        if datefmt:
            arrow_time = arrow_time.format(datefmt)
        return str(arrow_time)
logger = logging.getLogger(__name__)
default_handler = logging.StreamHandler()
default_handler.setFormatter(ArrowTimeFormatter(
    fmt='%(asctime)s',
    datefmt='YYYY-MM-DD HH:mm:ss.SSS'
))
logger.setLevel(logging.DEBUG)
logger.addHandler(default_handler)现在,您可以在属性中使用所有箭头的时间格式datefmt。
回答 7
tl; dr供在此处查找ISO格式日期的人员使用:
datefmt:’%Y-%m-%d%H:%M:%S.%03d%z’
回答 8
到目前为止,以下与python 3完美兼容。
         logging.basicConfig(level=logging.DEBUG,
                     format='%(asctime)s %(levelname)-8s %(message)s',
                     datefmt='%Y/%m/%d %H:%M:%S.%03d',
                     filename=self.log_filepath,
                     filemode='w')提供以下输出
2020/01/11 18:51:19.011信息

