3

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.

jeromej
  • 10,508
  • 2
  • 43
  • 62
  • 5
    Your code snippet works. Could it be that you called `logging.debug('some text')` already somehwere else before in your real code? This automatically configures the root logger and your `basicConfig` will be ignored. – SmCaterpillar Apr 24 '15 at 16:48
  • @SmCaterpillar Brillant sir! I did read something about that but I didn't understand it quite well, so that was it! Feel free to post it as an answer as well! – jeromej Apr 24 '15 at 16:51
  • 1
    You're welcome, since your answer already mentions the solutions, there's really no need for another one :-) – SmCaterpillar Apr 27 '15 at 07:40

1 Answers1

5

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.

jeromej
  • 10,508
  • 2
  • 43
  • 62