gpt4 book ai didi

python日志记录不保存到文件

转载 作者:太空狗 更新时间:2023-10-29 20:18:28 24 4
gpt4 key购买 nike

在过去的一个小时里,我一直被困在这个问题上。我已将登录插入我的 tkinter gui,但无法正常工作。然后我开始删除部分,直到我在非常 python 文档中找到了最基本的示例,它不会工作。此时我没有其他要删除的东西。

代码如下:

import logging

LOG_FILENAME = r'logging_example.out'


logging.basicConfig(filename=LOG_FILENAME ,level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')

f = open(LOG_FILENAME, 'rt')
try:
body = f.read()
finally:
f.close()

print('FILE:')
print (body)

警告打印到标准输出,但文件没有生成。我在 Windows 7 上运行 python 3.4,x64。它是一个 anacondas 发行版,所以它在 spyder 中的 Ipython 中运行。

我想这应该行得通

最佳答案

正如 Jonas Byström 所指出的,这在 Ipython 之外确实有效。在我有机会这样做之前,Ipython 似乎配置了一个日志记录处理程序。此外,如果处理程序已经存在,basicConfig 将不执行任何操作。所以,为了让它在 Ipython 中工作,必须做以下三件事之一:1) 添加一个新的处理程序,或者 2) 重新加载日志记录,或者 3) 删除现有的处理程序。我做了下面的 2 号。

import logging
from imp import reload
reload(logging)
LOG_FILENAME = r'logging_example.out'


logging.basicConfig(filename=LOG_FILENAME ,level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')

f = open(LOG_FILENAME, 'rt')
try:
body = f.read()
finally:
f.close()

print('FILE:')
print (body)

有关更多信息,请参阅以下内容: Logging in ipython ; More on the same

关于python日志记录不保存到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31169540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com