如果我有这个 python 代码:
import logging
logging.basicConfig(filename='allow.txt', level=logging.DEBUG, format='%(asctime)s')
logging.debug(2+3)
但是当我运行它时,我得到这样的结果,它甚至没有来自 3+2 的 5:
2018-10-20 10:06:45,177
如何让我的结果更像这样:
2018-10-20 10:06:45 : 5
甚至更好:
Date: 2018-10-20, Time: 10:06:45 Answer: 5
最佳答案
您需要在格式字符串中包含 %(message)s
以便记录日志消息:
logging.basicConfig(..., format='%(asctime)s : %(message)s')
您可以通过配置 datefmt
参数在时间戳中包含字符串“Date”和“Time”:
logging.basicConfig(
... ,
datefmt='Date: %Y-%m-%d, Time: %H:%M:%S',
format='%(asctime)s Answer: %(message)s',
)
关于Python 记录额外信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52908161/