Spyder IDE中的重复日志条目和锁定的日志文件 [英] Duplicate log entries and locked log files in Spyder IDE

查看:50
本文介绍了Spyder IDE中的重复日志条目和锁定的日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的是:我的python脚本运行,将日志记录消息输出到控制台和文件.

一旦python脚本完成运行,我希望能够删除/编辑日志文件.我在Windows7上使用Spyder IDE.

Once the python script finishes running, I want to be able to delete/edit the logging file. I am using Spyder IDE on Windows7.

示例代码:

import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

hdlr = logging.FileHandler("/Users/mds/Dropbox/_python/logger-debug.txt")
logger.addHandler(hdlr) 

logger.error("Am I duplicating error entries?")

hdlr.close()

我遇到的问题:

  1. 脚本完成运行后,文件上仍然有锁

  1. After the script finishes running, there is still a lock on the file

每次运行脚本时,日志文件都会增加许多重复的条目.

Each time I run the script the log file grows many duplicate entries.

我第一次运行脚本:

控制台:

runfile('C:/Users/mds/Dropbox/_python/logger-debugger.py', wdir='C:/Users/mds/Dropbox/_python')
ERROR:__main__:Am I duplicating error entries?

logger-debug.txt:

logger-debug.txt:

Am I duplicating error entries?

第二次运行脚本:控制台:

runfile('C:/Users/mds/Dropbox/_python/logger-debugger.py', wdir='C:/Users/mds/Dropbox/_python')
ERROR:__main__:Am I duplicating error entries?

logger-debug.txt

logger-debug.txt

Am I duplicating error entries?
Am I duplicating error entries?
Am I duplicating error entries?

我第三次运行脚本:

控制台:

runfile('C:/Users/mds/Dropbox/_python/logger-debugger.py', wdir='C:/Users/mds/Dropbox/_python')
ERROR:__main__:Am I duplicating error entries?

logger-debug.txt

logger-debug.txt

Am I duplicating error entries?
Am I duplicating error entries?
Am I duplicating error entries?
Am I duplicating error entries?
Am I duplicating error entries?
Am I duplicating error entries?

推荐答案

显然,仅关闭处理程序是不够的.还需要从记录器实例中将其删除.

apparently merely closing the handler is not enough. It also needs to be removed from the logger instance.

所以现在最后一个正确的例子是:

So now the final correct example is:

import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

"""remember to close this handler at finish!!!"""
hdlr = logging.FileHandler("/Users/mds/Dropbox/_python/logger-debug.txt")
logger.addHandler(hdlr) 


logger.error("Am I duplicating error entries?")

hdlr.close()
logger.removeHandler(hdlr)

注意新版本中的logger.removeHandler(hdlr).

因此,这解决了锁定文件的问题.它还解决了多次运行脚本多次写入同一日志消息的问题.现在很明显,这发生了,因为仍然有多个文件处理程序链接到该记录器实例(主要),因此许多处理程序同时编写了相同的错误消息.这意味着我不必必须使用覆盖文件模式,我也可以使用附加文件模式.

So this cured the problem of locked files. It also cured the problem of multiple runs of the script writing the same log messages several times. Now it's apparent that was happening because there were multiple filehandlers still linked to that logger instance (main), so many handlers simultaneously wrote the same error message. This means I do not have to use overwrite filemode, I can use append filemode too.

我看到了旧的堆栈溢出消息删除所有处理程序的代码集.

I saw an old Stack Overflow message that had this bit of code for deleting all handlers.

handlers = logger.handlers[:]
for handler in handlers:
    handler.close()
    logger.removeHandler(handler)

这篇关于Spyder IDE中的重复日志条目和锁定的日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆