1
我想從我的PyQt應用程序記錄所有錯誤。即使那些沉默 - 提出,但程序繼續運行。
所以,我有:捕獲PyQt例外
記錄功能
def setLogger(level=logging.DEBUG,
name="my_logger",
file=join("src", "log.out")):
logger = logging.getLogger(name)
logger.setLevel(level)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
formatter = logging.Formatter('%(message)s')
ch.setFormatter(formatter)
fh = logging.FileHandler(file)
fh.setLevel(level)
formatter = logging.Formatter(
'%(asctime)s : %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p')
fh.setFormatter(formatter)
logger.addHandler(ch)
logger.addHandler(fh)
return logger
Sys.excepthook的沉默異常
sys._excepthook = sys.excepthook
def exception_hook(exctype, value, traceback):
sys._excepthook(exctype, value, traceback)
sys.exit(1)
sys.excepthook = exception_hook
然後我嘗試做這樣的事情
def main():
app = QtGui.QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)
logger.info("New session started")
try:
window = MainWindow()
window.show()
sys.exit(app.exec_())
except Exception:
logger.exception("Got error in MainWindow")
而somwewhere在主窗口
try:
dialog = MyDialog(somedata, somedata)
dialog.closed.connect(self.doSomething)
dialog.exec_()
except Exception:
self.logger.exception("Got error in Dialog module")
sys.exit()
手動引發異常
class MyDialog(QtGui.QDialog):
def __init__(self):
super(MyDialog, self).__init__()
raise Exception
一切如預期......幾乎。
而不是消息我得到!
我在做什麼錯誤以及如何改變?