Mgamerz
Mgamerz

Reputation: 2890

Python Logger not respecting log level

Setup: Python 3.3

I have a unit test program that I want to print out log messages. Python is not printing anything but WARNING or higher messages through this logger I made, even though I set the log severity to INFO.

Here is the code I use:

test_logger = logging.getLogger('DesktopprTester')
print(test_logger.getEffectiveLevel())
test_logger.setLevel(logging.INFO)
print(test_logger.getEffectiveLevel())
test_logger.warning('WARN')
test_logger.info('INFO')

And here is the output:

30 <-30 is WARN
20 <-20 is INFO
WARN

It does not print INFO. So I am missing all my info statements in this file... and I am not sure why. The docs seem to agree with me. When I used to use the root logger, and set the level to info, it would print it (And all the other stuff it was doing). Now that I made my own logger, it doesn't seem to work. Note that WARNING level messages still come up, but they aren't what I want to see.

Is there something I am missing here? I feel like there's some obvious answer I'm not seeing...

Upvotes: 2

Views: 845

Answers (1)

loopbackbee
loopbackbee

Reputation: 23322

While running your code in python 2, you get a clear warning:

No handlers could be found for logger "DesktopprTester"

So you simply need to add a handler to your logger:

test_logger = logging.getLogger('DesktopprTester')
hdlr = logging.StreamHandler()
test_logger.addHandler(hdlr)

The warning doesn't appear on python 3 - I'm not sure why

Upvotes: 3

Related Questions