por*_*uod 7 python debugging logging stack-trace
当我使用标准的python日志记录模块有很多不同的模块时,下面的堆栈跟踪几乎没有帮助我找出确切地说,我有一个格式错误的日志语句:
Traceback (most recent call last):
File "/usr/lib/python2.6/logging/__init__.py", line 768, in emit
msg = self.format(record)
File "/usr/lib/python2.6/logging/__init__.py", line 648, in format
return fmt.format(record)
File "/usr/lib/python2.6/logging/__init__.py", line 436, in format
record.message = record.getMessage()
File "/usr/lib/python2.6/logging/__init__.py", line 306, in getMessage
msg = msg % self.args
TypeError: not all arguments converted during string formatting
Run Code Online (Sandbox Code Playgroud)
我只是开始使用python的日志模块,所以也许我忽略了一些明显的东西.我不确定堆栈跟踪是否无用,因为我正在使用greenlets,或者如果这对于日志记录模块来说是正常的,但任何帮助都将不胜感激.我愿意修改源代码,任何使日志库真正给出问题所在的线索的东西.
您可以找到如下错误,而不是编辑已安装的python代码:
def handleError(record):
raise RuntimeError(record)
handler.handleError = handleError
Run Code Online (Sandbox Code Playgroud)
handler是处理问题的处理程序之一.现在,当格式错误发生时,您将看到该位置.
日志记录模块旨在阻止不良日志消息杀死其余代码,因此该emit方法捕获错误并将其传递给方法handleError.您最简单的方法是暂时编辑/usr/lib/python2.6/logging/__init__.py和查找handleError.它看起来像这样:
def handleError(self, record):
"""
Handle errors which occur during an emit() call.
This method should be called from handlers when an exception is
encountered during an emit() call. If raiseExceptions is false,
exceptions get silently ignored. This is what is mostly wanted
for a logging system - most users will not care about errors in
the logging system, they are more interested in application errors.
You could, however, replace this with a custom handler if you wish.
The record which was being processed is passed in to this method.
"""
if raiseExceptions:
ei = sys.exc_info()
try:
traceback.print_exception(ei[0], ei[1], ei[2],
None, sys.stderr)
sys.stderr.write('Logged from file %s, line %s\n' % (
record.filename, record.lineno))
except IOError:
pass # see issue 5971
finally:
del ei
Run Code Online (Sandbox Code Playgroud)
现在暂时编辑它.raise在开始时插入一个简单应该确保错误得到代码传播而不是被吞下.解决问题后,只需将日志记录代码恢复到原来的状态即可.
| 归档时间: |
|
| 查看次数: |
1767 次 |
| 最近记录: |