I have spent far too much time on this and I couldn’t figure out by myself.
I am writing some code that use some modules, so I want to create a specific logger for my message, not those from the other modules (they are far too many so I don’t want to set a different level for each of them).
I have simplified my use case to:
JavaScript
x
12
12
1
import logging
2
3
logger = logging.getLogger('test')
4
logger.setLevel(level=logging.INFO)
5
print(logging.Logger.manager.loggerDict)
6
7
logger.debug('This is a debug message')
8
logger.info('This is an info message')
9
logger.warning('This is a warning message')
10
logger.error('This is an error message')
11
logger.critical('This is a critical message')
12
When I execute the previous code, I would expect to have my info message logged.
Instead I got:
JavaScript
1
6
1
python3 test.py
2
{'test': <Logger test (INFO)>}
3
This is a warning message
4
This is an error message
5
This is a critical message
6
Same with DEBUG
, it always print warning/error/critical messages, not the debug/info.
If I raise the logging level (let’s say ERROR
), it works as expected.
JavaScript
1
3
1
python --version
2
Python 3.9.1
3
Advertisement
Answer
Change the configuration at the package level / root logger (documentation).
JavaScript
1
2
1
logging.basicConfig(level=logging.INFO)
2