日志记录模块,ValueError:不完整的格式密钥

时间:2018-07-12 10:14:18

标签: python-3.x datetime logging

这是我的代码:

logging.basicConfig(filename='formsINFO.log', level=logging.INFO, format='%(%(levelname)s:%(message)s')

now = datetime.datetime.now()
s = now.strftime("%d %B %Y, %H:%M:%S")
logging.info("Time of testrun: {}".format(s))

这是错误:

--- Logging error ---
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\logging\__init__.py", line 992, in emit
    msg = self.format(record)
  File "C:\ProgramData\Anaconda3\lib\logging\__init__.py", line 838, in format
    return fmt.format(record)
  File "C:\ProgramData\Anaconda3\lib\logging\__init__.py", line 578, in format
    s = self.formatMessage(record)
  File "C:\ProgramData\Anaconda3\lib\logging\__init__.py", line 547, in formatMessage
    return self._style.format(record)
  File "C:\ProgramData\Anaconda3\lib\logging\__init__.py", line 391, in format
    return self._fmt % record.__dict__
ValueError: incomplete format key
Call stack:
  File "C:\Users\Form_Catch.py", line 38, in <module>
    logging.info("Time of testrun: {}".format(s))
Message: 'Time of testrun: 12 July 2018, 14:56:12'
Arguments: ()

我在Google上搜索了一个多小时,但找不到解决方案...添加formatter = logging.Formatter('%(levelname)-8s : %(message)s')(旧帖子中的解决方案)也不起作用。

0 个答案:

没有答案