sethserver / Python

Python Logging: Why Logger Defaults to WARN and How to Change It

By Seth Black Updated September 27, 2024

When debugging and profiling Python software you'll find yourself relying on Python's built-in logging module. Something that is very simple to overlook is the fact that the root logger defaults to WARN and not DEBUG. Note the output of the simple logging statements below.

import logging log = logging.getLogger(__name__) log.debug('this is debug') log.info('this is info') log.warning('this is warning') log.error('this is error') log.critical('this is really bad')

$ python3 testlogging.py this is warning this is error this is really bad

The root logger only prints out warnings, errors and criticalities. To increase the logging level set the logging.basicConfig to DEBUG.

import logging logging.basicConfig(level=logging.DEBUG) log = logging.getLogger(__name__) log.debug('this is debug') log.info('this is info') log.warning('this is warning') log.error('this is error') log.critical('this is really bad')

$ python3 testlogging.py DEBUG:__main__:this is debug INFO:__main__:this is info WARNING:__main__:this is warn ERROR:__main__:this is error CRITICAL:__main__:this is really bad

You'll also notice that the basicConfig changes the log format to include the log message type and log name.

Good luck and happy logging!

-Sethers