Python 日志记录:在时间格式中使用毫秒 [英] Python logging: use milliseconds in time format

查看:37
本文介绍了Python 日志记录:在时间格式中使用毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下 logging.Formatter('%(asctime)s') 使用以下格式打印:

By default logging.Formatter('%(asctime)s') prints with the following format:

2011-06-09 10:54:40,638

其中 638 是毫秒.我需要将逗号更改为点:

where 638 is the millisecond. I need to change the comma to a dot:

2011-06-09 10:54:40.638

要格式化我可以使用的时间:

To format the time I can use:

logging.Formatter(fmt='%(asctime)s',datestr=date_format_str)

然而,文档 没有指定如何格式化毫秒.我找到了 这个 SO问题 讨论微秒,但是 a) 我更喜欢毫秒和 b) 由于 %f,以下在 Python 2.6(我正在研究)上不起作用:

however the documentation doesn't specify how to format milliseconds. I've found this SO question which talks about microseconds, but a) I would prefer milliseconds and b) the following doesn't work on Python 2.6 (which I'm working on) due to the %f:

logging.Formatter(fmt='%(asctime)s',datefmt='%Y-%m-%d,%H:%M:%S.%f')

推荐答案

这也应该有效:

logging.Formatter(fmt='%(asctime)s.%(msecs)03d',datefmt='%Y-%m-%d,%H:%M:%S')

这篇关于Python 日志记录:在时间格式中使用毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆