簡體   English   中英

Python日志記錄不會關閉

[英]Python logging wont shutdown

我一直在學習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

根據文檔,該命令應該“通過刷新和關閉所有處理程序來執行有序關閉”。 我應該做其他事情來關閉日志文件嗎?

所以我發現使用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指定:

這應該在應用程序出口處調用,並且在此調用之后不應再使用日志記錄系統。

這並不意味着之后不能使用記錄器對象。 在調用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

有任何實際的解決方法嗎?

每次調用函數時,我都會遇到同樣的問題。 我找到的解決方案是在執行代碼后清除所有處理程序

log.handlers.clear()

不知道這是不是正確的方法,但它對我有用。

我在編寫tKinter應用程序時遇到了這個問題。 由於正在使用各種控件,每次我(例如)連續多次按下某個按鈕時,我進行了大量的記錄,記錄行的次數等於我連續按下按鈕的次數。

我在main()的末尾添加了: log.handlers.clear() ,它清除了問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM