相关疑难解决方法(0)

如何使用调试信息记录Python错误?

我正在将Python异常消息打印到日志文件中logging.error:

import logging
try:
    1/0
except ZeroDivisionError as e:
    logging.error(e)  # ERROR:root:division by zero
Run Code Online (Sandbox Code Playgroud)

是否可以打印有关异常的更详细信息以及生成它的代码而不仅仅是异常字符串?线号或堆栈跟踪之类的东西会很棒.

python logging exception-handling exception

417
推荐指数
10
解决办法
28万
查看次数

'sys.excepthook'和线程

我正在使用Python 2.5并尝试excepthook在我的程序中使用自定义.在主线程中,它完美地运行.但是在一个以线程模块开始的线程中,通常excepthook会调用它.

这是一个显示问题的示例.取消注释注释会显示所需的行为.

import threading, sys

def myexcepthook(type, value, tb):
    print 'myexcepthook'

class A(threading.Thread, object):

    def __init__(self):
        threading.Thread.__init__(self, verbose=True)
#       raise Exception('in main')
        self.start()

    def run(self):
        print 'A'
        raise Exception('in thread')            

if __name__ == "__main__":
    sys.excepthook = myexcepthook
    A()
Run Code Online (Sandbox Code Playgroud)

那么,我如何excepthook在一个线程中使用自己的?

python multithreading exception-handling exception

18
推荐指数
3
解决办法
5587
查看次数

需要 py.test 从 python 日志模块记录日志文件中的断言错误

需要 py.test 从 python 日志模块记录日志文件中的断言错误。该测试设置了 python 日志记录模块,所有日志都按预期进行。我在整个测试中使用了断言语句。但是当遇到断言错误时,这些消息不会记录在 python 日志输出中,而是记录在命令控制台中。

有没有办法让 py.test 在测试的日志输出中记录断言错误?

Right now the errors are in command console but it would be great if these assertion errors are also logged as part of python logging output so all the log messages are captured in one place. Also, for long running test, I cannot see the errors until the entire test finish which could be a long time to wait. It would be great if I can see the …

python assert pytest

1
推荐指数
1
解决办法
3184
查看次数