Python: Logging TypeError: not all arguments converted during string formatting

9 浏览
0 Comments

Python: Logging TypeError: not all arguments converted during string formatting

问题的快速复现如下:

>>> import logging
>>> logging.getLogger().setLevel(logging.INFO)
>>> from datetime import date
>>> date = date.today()
>>> logging.info('date={}', date)
Traceback (most recent call last):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 846, in emit
    msg = self.format(record)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 723, in format
    return fmt.format(record)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 464, in format
    record.message = record.getMessage()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 328, in getMessage
    msg = msg % self.args
TypeError: not all arguments converted during string formatting
Logged from file , line 1
>>> 

如何使其正常工作?


这实际上是为什么我在使用%时得到"TypeError: not all arguments converted during string formatting",尝试替换类似{0}的占位符?的特殊情况,但由于实际的格式化步骤发生在用户的代码之外,所以需要使用不同的解决方法。

0