Python日志记录不会关闭

2024-05-17 16:23:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在学习python日志模块,但在完成后关闭日志时遇到了问题。下面是一个例子-

import logging

log = logging.getLogger()
log.setLevel(logging.INFO)
handler = logging.FileHandler('test.log')
handler.setLevel(logging.INFO)
formatter = logging.Formatter(
            fmt='%(asctime)s %(levelname)s: %(message)s',
            datefmt='%Y-%m-%d %H:%M:%S'
            )
handler.setFormatter(formatter)
log.addHandler(handler)

log.info('log file is open')  
logging.shutdown()
log.info('log file should be closed')

但是模块在logging.shutdown()之后仍在记录,因为日志文件如下-

# cat test.log
2014-07-17 19:39:35 INFO: log file is open
2014-07-17 19:39:35 INFO: log file should be closed

根据文档,该命令应“通过刷新和关闭所有处理程序执行有序关闭”。我应该做些别的事情来关闭日志文件吗?


Tags: 模块testinfologisformatterloggingbe
3条回答

因此,我发现使用shutdown()并不能完全消除用于日志记录的文件处理程序。最好的方法似乎是手动删除文件处理程序-

def main():
    log = logging.getLogger()
    log.setLevel(logging.INFO)
    fh = logging.FileHandler(filename='test.log')
    fh.setLevel(logging.INFO)
    formatter = logging.Formatter(
                    fmt='%(asctime)s %(levelname)s: %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S'
                    )
    fh.setFormatter(formatter)
    log.addHandler(fh)

    log.info('-------Start--------')
    log.info('this function is doing something')
    log.info('this function is finished')
    log.removeHandler(fh) <--------------------------Add this line
    del log,fh

shutdownPython Doc指定:

This should be called at application exit and no further use of the logging system should be made after this call.

这并不意味着记录器对象不能在之后使用。在调用shutdown之后,不要试图使用该logger对象记录任何其他内容,这应该是正常的。

所以这就是我现在的挣扎。我有一条运行守护进程的python。守护进程定期调用函数。函数每次运行时都应该打开日志文件,写入内容并将其关闭。因为它作为守护进程运行,所以它从不关闭程序。这是测试代码。

import logging

def main():
    log = logging.getLogger()
    log.setLevel(logging.INFO)
    handler = logging.FileHandler(filename='test.log')
    handler.setLevel(logging.INFO)
    formatter = logging.Formatter(
                    fmt='%(asctime)s %(levelname)s: %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S'
                    )
    handler.setFormatter(formatter)
    log.addHandler(handler)

    log.info('-------Start--------')
    log.info('this function is doing something')
    log.info('this function is finished')
    logging.shutdown()
    del log

但每次我运行这个函数,它都会创建一个重复的日志条目,就像-

>>> main()
>>> main()
>>> main()
>>> main()

$ cat test.log
2014-07-19 11:16:21 INFO: -------Start--------
2014-07-19 11:16:21 INFO: this function is doing something
2014-07-19 11:16:21 INFO: this function is finished
2014-07-19 11:16:23 INFO: -------Start--------
2014-07-19 11:16:23 INFO: -------Start--------
2014-07-19 11:16:23 INFO: this function is doing something
2014-07-19 11:16:23 INFO: this function is doing something
2014-07-19 11:16:23 INFO: this function is finished
2014-07-19 11:16:23 INFO: this function is finished
2014-07-19 11:16:25 INFO: -------Start--------
2014-07-19 11:16:25 INFO: -------Start--------
2014-07-19 11:16:25 INFO: -------Start--------
2014-07-19 11:16:25 INFO: this function is doing something
2014-07-19 11:16:25 INFO: this function is doing something
2014-07-19 11:16:25 INFO: this function is doing something
2014-07-19 11:16:25 INFO: this function is finished
2014-07-19 11:16:25 INFO: this function is finished
2014-07-19 11:16:25 INFO: this function is finished
2014-07-19 11:16:28 INFO: -------Start--------
2014-07-19 11:16:28 INFO: -------Start--------
2014-07-19 11:16:28 INFO: -------Start--------
2014-07-19 11:16:28 INFO: -------Start--------
2014-07-19 11:16:28 INFO: this function is doing something
2014-07-19 11:16:28 INFO: this function is doing something
2014-07-19 11:16:28 INFO: this function is doing something
2014-07-19 11:16:28 INFO: this function is doing something
2014-07-19 11:16:28 INFO: this function is finished
2014-07-19 11:16:28 INFO: this function is finished
2014-07-19 11:16:28 INFO: this function is finished
2014-07-19 11:16:28 INFO: this function is finished

有什么切实可行的方法来解决这个问题吗?

相关问题 更多 >