Question about logging.config.dictConfig

Ivan "Rambius" Ivanov rambiusparkisanius at gmail.com
Tue Feb 7 17:58:26 EST 2023


Hello,

I am trying to configure my loggers using dictConfig, but they do not
print anything. Here are more details.

I have several python scripts that use a similar logging setup. I put
the common configuration in a separate module myloggingconf.py:

# myloggingconf.py
import logging

def configure_logging():
    default = logging.Formatter('%(asctime)s %(levelname)-7s %(name)s
%(funcName)s %(message)s')

    consoleHandler = logging.StreamHandler()
    consoleHandler.setFormatter(default)

    rootLogger = logging.getLogger()
    rootLogger.addHandler(consoleHandler)
    rootLogger.setLevel(logging.INFO)

Then I use it in the other scripts as:

# uselog.py
import logging
import os

from myloggingconf import configure_logging

logger = logging.getLogger(os.path.basename(__file__))

def dosmth():
    logger.debug("debug")
    logger.info("info")
    logger.warning("warning")
    logger.error("error")

def main():
    configure_logging()
    dosmth()

if __name__ == '__main__':
    main()

This works correctly and the log messages about INFO print out. Now I
want to try dictConfig. I changed myloggingconf.configure_logging to:

# myloggingconf.py
import logging.config

def configure_logging():
    config = {
        'version': 1,
        'disable_existing_loggers': True,
        'formatters': {
            'default': {
                'fmt': '%(asctime)s %(levelname)-7s %(name)s
%(funcName)s %(message)s'
            }
        },
        'handlers': {
            'stdout': {
                'level': 'DEBUG',
                'class': 'logging.StreamHandler',
                'formatter': 'default',
            }
        },
        'loggers': {
            '': {
                'handlers': ['stdout'],
                'level': 'DEBUG'
            }
        }
    }
    logging.config.dictConfig(config)

When I run uselog.py it prints nothing. I am wondering what is wrong
with the second configuration. I will appreciate any help.

Regards
rambius

-- 
Tangra Mega Rock: http://www.radiotangra.com


More information about the Python-list mailing list