3

I have an issue with Python logging and I'm not sure what the problem is as the same line of code used to work just fine yesterday.

So for example the following code only produces output for the print function but not for logging.

Any ideas?

import logging

if __name__ == '__main__':
    logging.basicConfig(level = logging.DEBUG)
    logging.info("Hello, World!")
    print "Hello, World!"
2
  • 1
    INFO:root:Hello, World! Hello, World! works for me, which python version Commented Jan 15, 2013 at 10:22
  • @avasal Version 2.7.3 and the strange thing is that it was working fine yesterday. Commented Jan 15, 2013 at 10:52

2 Answers 2

2

You can try this alternative:

>>> import logging
>>> logging.getLogger().setLevel(logging.INFO)
>>> logging.info("Hello, World!")
INFO:root:Hello, World!

Here you are setting to the root logger the info level.

Sign up to request clarification or add additional context in comments.

3 Comments

@Gabor: Setting it to DEBUG level should include anything on INFO level as well, so there is something additional going on here.
@LennartRegebro I think the problem is that logging.basicConfig is not configuring correctly the logger. From logging documentation "This function does nothing if the root logger already has handlers configured for it."
Right, so in the actual application, logging is already configured elsewhere.
0

Works for me:

>>> import logging
>>> logging.basicConfig(level = logging.DEBUG)
>>> logging.info("Hello, World!")
INFO:root:Hello, World!

Also your code works in a file:

INFO:root:Hello, World!
Hello, World!

1 Comment

It works when I type in the code line by line onto the python interpreter, but it does not work when I try to run the file containing those statements

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.