gpt4 book ai didi

python 日志记录根本不起作用

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

我正在尝试在我的小型 Python 项目中使用日志记录。按照教程,我将下面的代码添加到我的代码中,但是消息没有按预期记录到文件中。

import logging
logging.basicConfig(
filename = "a.log",
filemode="w",
level = logging.DEBUG)
logging.error("Log initialization failed.")

密码中没有创建日志文件。 (我已经使用下面的代码打印出密码,我确定我检查了正确的目录。)所以我手动创建了文件并运行了代码,但消息仍然没有被记录。

print "argv: %r"%(sys.argv,)
print "dirname(argv[0]): %s"%os.path.abspath(os.path.expanduser(os.path.dirname(sys.argv[0])))
print "pwd: %s"%os.path.abspath(os.path.expanduser(os.path.curdir))

有人知道我在这里做错了什么吗?提前致谢。

最佳答案

您至少调用了两次 basicConfig();第一次没有文件名。清除处理程序并重试:

logging.getLogger('').handlers = []

logging.basicConfig(
filename = "a.log",
filemode="w",
level = logging.DEBUG)

关于python 日志记录根本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15167348/

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