我正在编写一些想要与我的团队共享的脚本,因此我一直在构建一堆日志记录,以便在他们在某个地方遇到崩溃时更容易进行调试,从那时起我就可以看到到底发生了什么崩溃。
一般记录到文件没有问题,但我有一个未捕获的异常问题。我尝试了各种方法来让它工作,例如this https://stackoverflow.com/questions/6234405/logging-uncaught-exceptions-in-python and this https://stackoverflow.com/questions/20714644/python-sys-excepthook-and-logging-uncaught-exceptions-across-multiple-modules。当我从 IDLE 或命令提示符运行它时,似乎 sys.excepthook 没有被调用。
昨天,如果异常发生在主模块中,它会正确记录,但如果异常发生在导入的类中,则不会正确记录。现在异常根本不会被记录,我不知道我改变了什么(所以我今天安装了 Git :-P)
这是我试图开始工作的代码,即主模块:
import tkinter as tk
import sys
import traceback
import logging
import datetime
import exception_logging_test_imported_class as impclass
# Main Class
class ExceptMain(tk.Frame):
def __init__(self, parent):
logging.info('Start main')
tk.Frame.__init__(self, parent, relief='groove', bd=4)
self.parent = parent
self.pack()
tk.Label(self, text='This is the main class', bg='white').pack()
tk.Button(self, text='Start subframe', command=self.run).pack()
tk.Button(self, text='Throw main exception', command=self.throwex).pack()
tk.Button(self, text='Start imported class', command=self.start_import).pack()
# Function to start another frame, from this same file
def run(self):
logging.info('Run main function')
subclass = ExceptSubclass(self)
subclass.pack()
# Function to start an imported frame class
def start_import(self):
imported_class = impclass.ExtraClass(self)
imported_class.pack()
# Throw an exception
def throwex(self):
raise ValueError("Main is burning down")
#Another class in this file
class ExceptSubclass(tk.Frame):
def __init__(self, parent):
logging.info('Start subframe')
tk.Frame.__init__(self, parent, relief='groove', bd=4)
self.parent = parent
self.pack()
tk.Label(self, text='This is the subclass', bg='white').pack()
tk.Button(self, text='run sub function', command=self.script).pack()
tk.Button(self, text='Throw sub exception', command=self.throwexsub).pack()
# Run something
def script(self):
logging.info('Run subfunction')
tk.Label(self, text='Script has run').pack()
# Throw an exception
def throwexsub(self):
thing = []
thing[1]
# Use a logger object or just logging
logger_not_just_logging = False
if logger_not_just_logging:
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.FileHandler("ZZ_logging_test.log"))
else:
logging.basicConfig(filename='ZZ_logging_test.log', level=logging.DEBUG)
logging.info('<<<<< STARTING NEW INSTANCE >>>>>')
# This function needs to be called somehow when an uncaught exception happens
def exception_handler(etype, value, tb):
logging.exception("Uncaught exception: {0}".format(str(value)))
with open('ZZ_exception_test.log', 'a') as file:
file.write("<<< Exception occurred at " + str(datetime.datetime.now()) + " >>>")
traceback.print_exception(etype, value, tb, file=file)
# Install the hook
sys.excepthook = exception_handler
# Run the app
root = tk.Tk()
app = ExceptMain(root)
root.mainloop()
还有一个子模块:
import tkinter as tk
import logging
# This is an imported frame
class ExtraClass(tk.Frame):
def __init__(self, parent):
logging.info('Start imported frame')
tk.Frame.__init__(self, parent, relief='groove', bd=4)
self.parent = parent
self.pack()
tk.Label(self, text='This is an imported frame', bg='white').pack()
tk.Button(self, text='run imported frame function', command=self.script).pack()
tk.Button(self, text='Throw imported exception', command=self.throwexsub).pack()
# Imported frame has a function
def script(self):
logging.info('Run imported frame function')
tk.Label(self, text='Imported script has run').pack()
# Imported frame throws an exception
def throwexsub(self):
thing = []
thing[1]
我期望得到的是一个包含所有常规日志记录的文件和一个包含异常和堆栈跟踪的单独文件。
如果您能帮我解决这个问题,我将永远感激不已!