【发布时间】:2017-01-26 21:33:30
【问题描述】:
假设我得到了这个logging.logger 实例:
import logging
logger = logging.getLogger('root')
FORMAT = "[%(filename)s:%(lineno)s - %(funcName)20s() ] %(message)s"
logging.basicConfig(format=FORMAT)
logger.setLevel(logging.DEBUG)
当我尝试像使用带有动态数量参数的内置打印一样使用它时,问题就来了:
>>> logger.__class__
<class 'logging.Logger'>
>>> logger.debug("hello")
[<stdin>:1 - <module>() ] hello
>>> logger.debug("hello","world")
Traceback (most recent call last):
File "c:\Python2711\Lib\logging\__init__.py", line 853, in emit
msg = self.format(record)
File "c:\Python2711\Lib\logging\__init__.py", line 726, in format
return fmt.format(record)
File "c:\Python2711\Lib\logging\__init__.py", line 465, in format
record.message = record.getMessage()
File "c:\Python2711\Lib\logging\__init__.py", line 329, in getMessage
msg = msg % self.args
TypeError: not all arguments converted during string formatting
Logged from file <stdin>, line 1
如何模拟仍在使用 logging.Logger 的打印行为?
【问题讨论】:
-
只需将参数括在括号中,例如 logger.debug(("hello","world"))
-
安德烈:嗯,好的,我可以按照你的模式快速搜索和替换我所有的印刷品,干杯;-)
标签: python python-3.x logging