默认情况下,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)
但是,documentation并没有指定如何格式化毫秒。我已经找到了this SO question,它讲的是微秒,但a)我更喜欢毫秒,b)由于%f
的原因,以下代码在Python2.6(我正在开发的版本)上不起作用
logging.Formatter(fmt='%(asctime)s',datefmt='%Y-%m-%d,%H:%M:%S.%f')
发布于 2011-09-22 23:33:48
这也应该行得通:
logging.Formatter(fmt='%(asctime)s.%(msecs)03d',datefmt='%Y-%m-%d,%H:%M:%S')
发布于 2018-03-12 14:33:53
添加msecs是更好的选择,谢谢。以下是我在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")
发布于 2017-02-26 05:42:53
我发现的最简单的方法是覆盖default_msec_format:
formatter = logging.Formatter('%(asctime)s')
formatter.default_msec_format = '%s.%03d'
https://stackoverflow.com/questions/6290739
复制相似问题