比方說,我得到這個logging.logger實例:如何讓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的打印行爲?
只是附上PARAMS像logger.debug括號(( 「你好」, 「世界」)),例如 – Andrey
安德烈:嗯,好吧,我可以讓快速搜索和替換我的所有打印後,您的模式,歡呼聲;-) – BPL