简体   繁体   中英

Root logger ignoring logger level

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM