jroz
jroz

Reputation: 123

logging fails to configure some imported modules (including imports) (python logging)

I want to print all logging messages from all imported modules. Certain imported modules are not logging.

Note that all the files in the libraries I care about have calls logger = logging.getLogger(__name__) at the top.

Sorry if this is an easy problem. I've looked through a lot of posts without success.

I observe that the loggers for some modules are not being updated by the call to basicConfig

import logging
import local_util   # a local util file
from transformers import T5ForConditionalGeneration

for n in logging.root.manager.loggerDict:
    print(logging.getLogger(n))

logging.basicConfig(level=logging.DEBUG)
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
formatter = logging.Formatter("[%(asctime)s] %(levelname)s::%(module)s::%(funcName)s() %(message)s")
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)

for n in logging.root.manager.loggerDict:
    print(logging.getLogger(n))

In the first call to print(logging.getLogger(n)) almost all loggers are set with level WARNING.

In the second call, most loggers are set to DEBUG (including the one for local_util), except for the transformers library which all remain at level WARNING.

I can get transformers messages to print if I manually cycle through all loggers and reset their levels. Even if I use force=True in the call to basicConfig, the loggers for the transformers library do not get updated.

Upvotes: 0

Views: 162

Answers (1)

dragon2fly
dragon2fly

Reputation: 2429

basicConfig means to affect the root logger. If other child loggers don't set their level (which is default to NOTSET), they use the level of the root logger.

So if transformers has set a WARNING level on its own, it won't use DEBUG level of root.

You can set its level directly as the following:

transformers_logger = logging.getLogger('transformers')
transformers_logger.setLevel(logging.DEBUG) 

Upvotes: 0

Related Questions