【发布时间】:2023-02-21 00:19:07
【问题描述】:
出于某种原因,我的 Python 记录器不想识别微秒格式。
import logging, io
stream = io.StringIO()
logger = logging.getLogger("TestLogger")
logger.setLevel(logging.INFO)
logger.propagate = False
log_handler = logging.StreamHandler(stream)
log_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s',"%Y-%m-%d %H:%M:%S.%f %Z")
log_handler.setFormatter(log_format)
logger.addHandler(log_handler)
logger.info("This is test info log")
print(stream.getvalue())
它返回:
2023-01-06 18:52:34.%f UTC - TestLogger - INFO - This is test info log
为什么缺少微秒?
更新
我在跑步 蟒蛇 3.10.4 发行商 ID:Debian 描述:Debian GNU/Linux 11(靶心) 发布:11 代号:牛眼
【问题讨论】:
-
在我的系统 (Python 3.9.7) 上,示例程序失败并出现错误“值错误:格式字符串无效”。如果我从格式字符串中删除“.%f”,程序将毫无怨言地运行(但当然只打印整个秒值,没有小数部分)
-
既然你提到了它,我似乎找不到任何明确的文档声称微秒分辨率可用于 python 日志记录。
标签: python logging time format