假设我有一个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)
当我尝试像内置的print一样使用它,并具有动态数量的参数时,问题就出现了:
>>> 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模拟打印行为?