python日志记录根本不起作用 [英] python logging does not work at all
问题描述
我正在尝试在我的小型python项目中使用日志记录.在学习完本教程之后,我将下面的代码添加到了我的代码中,但是该消息并没有按预期的那样记录到文件中.
I am trying to use logging in my small python project. Following the tutorial, I added the code below to my code, but the message wan't logged to the file as it was supposed to.
import logging
logging.basicConfig(
filename = "a.log",
filemode="w",
level = logging.DEBUG)
logging.error("Log initialization failed.")
在pwd中没有创建日志文件. (我已使用以下代码打印出pwd,并且确定可以检查正确的目录.)因此,我手动创建了文件并运行了该代码,但该消息仍未记录.
There was no log file created in the pwd. (I have used the following code to print out the pwd, and I am sure I checked the right directory.) So I manually created the file and ran the code, but the message was still not logged.
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))
有人知道我在这里做错了什么吗?预先感谢.
Has someone any clue what I did wrong here? Thanks in advance.
推荐答案
您至少打过两次电话basicConfig()
;第一次没有文件名.清除处理程序,然后重试:
You called basicConfig()
twice at least; the first time without a filename. Clear the handlers and try again:
logging.getLogger('').handlers = []
logging.basicConfig(
filename = "a.log",
filemode="w",
level = logging.DEBUG)
这篇关于python日志记录根本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!