Python 如何获取日志类型错误“字符串格式化期间未转换所有参数”的行号?
如果我使用内置python日志机制,并且犯了一个错误,例如:Python 如何获取日志类型错误“字符串格式化期间未转换所有参数”的行号?,python,exception,logging,error-handling,error-logging,Python,Exception,Logging,Error Handling,Error Logging,如果我使用内置python日志机制,并且犯了一个错误,例如: logger.debug("The result is", result) 然后我得到一条毫无帮助的错误消息: Traceback (most recent call last): File "/usr/lib/python2.6/logging/__init__.py", line 760, in emit msg = self.format(record) File "/usr/lib/python2.6/logging/__i
logger.debug("The result is", result)
然后我得到一条毫无帮助的错误消息:
Traceback (most recent call last):
File "/usr/lib/python2.6/logging/__init__.py", line 760, in emit
msg = self.format(record)
File "/usr/lib/python2.6/logging/__init__.py", line 644, in format
return fmt.format(record)
File "/usr/lib/python2.6/logging/__init__.py", line 432, in format
record.message = record.getMessage()
File "/usr/lib/python2.6/logging/__init__.py", line 302, in getMessage
msg = msg % self.args
TypeError: not all arguments converted during string formatting
鉴于我有大量的日志记录语句,是否有任何方法可以获得更有用的错误消息—显示出错行号的消息?多亏了,这很容易做到。无论在何处设置日志代码,请执行以下操作:
import logging
def handleError(self, record):
raise
logging.Handler.handleError = handleError
堆栈跟踪中的某个地方将出现对logger.debug的错误调用。请注意警告:
请注意,仅插入这样的错误处理程序并不是您想要部署的,因为日志记录中的错误应该会引入应用程序错误。它实际上更多的是为了确保您首先获得正确的日志消息。在将此错误转储代码永久保留在应用程序中之前,请确保阅读并理解有关handleError稍后在消息线程中如何工作的注释
在Python的最新版本中,将打印您想要的信息。考虑下面的脚本,LogEX.PY: 使用Python 2.7运行时:
$ python logex.py
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 842, in emit
msg = self.format(record)
File "/usr/lib/python2.7/logging/__init__.py", line 719, in format
return fmt.format(record)
File "/usr/lib/python2.7/logging/__init__.py", line 464, in format
record.message = record.getMessage()
File "/usr/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 logex.py, line 6
使用Python 3.2:
$ python3.2 logex.py
Traceback (most recent call last):
File "/usr/lib/python3.2/logging/__init__.py", line 937, in emit
msg = self.format(record)
File "/usr/lib/python3.2/logging/__init__.py", line 812, in format
return fmt.format(record)
File "/usr/lib/python3.2/logging/__init__.py", line 551, in format
record.message = record.getMessage()
File "/usr/lib/python3.2/logging/__init__.py", line 319, in getMessage
msg = msg % self.args
TypeError: not all arguments converted during string formatting
Logged from file logex.py, line 6
因此,除非您使用的是较旧版本的Python,否则您不应该像Claudiu的回答中建议的那样使用任何技巧。哦,我不知道!是的,我正在使用Python 2.6。
$ python3.2 logex.py
Traceback (most recent call last):
File "/usr/lib/python3.2/logging/__init__.py", line 937, in emit
msg = self.format(record)
File "/usr/lib/python3.2/logging/__init__.py", line 812, in format
return fmt.format(record)
File "/usr/lib/python3.2/logging/__init__.py", line 551, in format
record.message = record.getMessage()
File "/usr/lib/python3.2/logging/__init__.py", line 319, in getMessage
msg = msg % self.args
TypeError: not all arguments converted during string formatting
Logged from file logex.py, line 6