Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python logging does not work at all

Tags:

python

logging

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.")

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.

like image 562
MYP Avatar asked Mar 01 '13 21:03

MYP


1 Answers

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)
like image 50
Martijn Pieters Avatar answered Sep 30 '22 20:09

Martijn Pieters