Gabor
Gabor

Reputation: 298

Missing logging output in Python

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!"

Upvotes: 3

Views: 4097

Answers (2)

Lennart Regebro
Lennart Regebro

Reputation: 172199

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!

Upvotes: 0

jvallver
jvallver

Reputation: 2340

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.

Upvotes: 2

Related Questions