Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Root logger ignoring logger level

Tags:

python

logging

Root logger doesn't log when (I think) it should:

import logging

# NOTE: I make sure to set the root logger level to logging.DEBUG
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)

logging.debug('This is a debugging test.')

From my understanding this should log something but it does nothing. Quick Google searches didn't help me to figure this issue out, neither did the official documentation.

In the other hand, if I use logging.warning instead of logging.debug, it does work.

What am I doing wrong?

EDIT:

Checking the current level with logging.getLogger().getEffectiveLevel() indicates me that the level is still at 30, like before the call of logging.basicConfig.

Checking logging.getLogger().isEnabledFor(logging.DEBUG) effectively tells me that the root logger level isn't enabled for logging.DEBUG.

like image 272
jeromej Avatar asked Dec 05 '25 10:12

jeromej


1 Answers

EDIT: Turned out OP's (me) trouble was due to already calling logging.debug before logging.basicConfig, thus settings the config to defaults and the call to logging.basicConfig was ignored as pointed out by @SmCaterpillar.

Although, this alternative solution allows you to "force" root logger level after a basicConfig has already been set, could still be useful!


Found a "solution":

# […] Previous code

# Sets the root logger level
logging.getLogger().setLevel(logging.DEBUG)

logging.debug('Test')  # Displays 'DEBUG:root:Test'

But it doesn't carry out the format I gave in in logging.basicConfig.

That said I'm already satisfied: It is already some kind of progress. Logging even with not exactly the format I expected is always better than no logging at all.

like image 186
jeromej Avatar answered Dec 06 '25 22:12

jeromej



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!