日志记录不会覆盖现有的日志文件

时间:2017-07-19 22:10:59

标签: python python-3.x logging exception-handling

我不熟悉日志记录,而且我正在尝试编写一些代码来记录我的某个程序中未捕获的异常。我已阅读文档,出于某种原因,我无法做任何事情,除了附加到日志文件,这不是我想要的。我指定filemode='w'无济于事。任何人都知道我在这种情况下做错了什么?

这是我的错误处理程序,它包含我的日志代码(我尝试将logging.basicConfig放在函数之外,但似乎没有影响任何内容):

### Function that handles unexpected errors and dumps relevant information to a log file.
def exceptionhandler(etype, value, tb):
    # Checks if exception was manually raised.
    global has_been_called
    # If exception was not raised manually then it is uncaught.
    if has_been_called is not True:
            excmessage = 'Uncaught exception:\n'
            excmessage += ''.join(traceback.format_exception(etype, value, tb))
            tkinter.messagebox.showwarning(title="ERROR: Please send screenshot to Dev Team.",
                                           message=excmessage)
            logging.basicConfig(filename='Error.log', 
                    filemode='w',
                    format='%(asctime)s: %(message)s',
                    level=DEBUG)
            logging.debug(excmessage)
    # Reset has_been_called to its default value.
    else:
        has_been_called = False

0 个答案:

没有答案