CodeBlanks Logo Icon CodeBlanks

dictConfig

Learn advanced config with logging.config.dictConfig.

dictConfig

logging.config.dictConfig sets up a complex logging configuration from a dict. Handlers, formatters and loggers are defined separately; this is good for working with configuration loaded from a file.

import logging.config

config = {
    "version": 1,
    "formatters": {"simple": {"format": "%(levelname)s:%(message)s"}},
    "handlers": {"console": {"class": "logging.StreamHandler", "formatter": "simple"}},
    "loggers": {"myapp": {"handlers": ["console"], "level": "DEBUG"}},
}
logging.config.dictConfig(config)

version: 1 is always given. The dict structure lets you manage many loggers and handlers from one place.

Dict sections

The dictConfig dict has four main sections: formatters, handlers, loggers and root. Each is a dict of dicts.

config = {
    "version": 1,
    "formatters": {"simple": {...}},
    "handlers": {"console": {...}},
    "loggers": {"myapp": {...}},
}

A handler refers to a formatter by name; a logger refers to a handler by name. This makes components reusable.

Practice Log with dictConfig

Start with import logging.config. Define a dict config: "version": 1, "formatters": {"simple": {"format": "%(levelname)s:%(message)s"}}, "handlers": {"console": {"class": "logging.StreamHandler", "formatter": "simple"}}, "loggers": {"myapp": {"handlers": ["console"], "level": "DEBUG"}}. Call logging.config.dictConfig(config). Get a logger with logger = logging.getLogger("myapp"). Also add the import logging line. Write logger.info("merhaba").

Starter code

main.py
# bring in logging.config and logging
# define config dict: version, formatters, handlers, loggers
# load the configuration with dictConfig
# get the 'myapp' logger
# write 'merhaba' at info level

Sample solution

main.py
import logging
import logging.config

config = {
    "version": 1,
    "formatters": {"simple": {"format": "%(levelname)s:%(message)s"}},
    "handlers": {"console": {"class": "logging.StreamHandler", "formatter": "simple"}},
    "loggers": {"myapp": {"handlers": ["console"], "level": "DEBUG"}},
}
logging.config.dictConfig(config)
logger = logging.getLogger("myapp")
logger.info("merhaba")

Expected output

INFO:merhaba

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Complete the dictConfig call

Question
import logging.config

logging.config.____(config)
Answer
import logging.config

logging.config.dictConfig(config)

Explanation: logging.config.dictConfig(config) loads the configuration.

2 What does the 'version' key do in a dictConfig dict?

  • Specifies the logger name
  • Specifies the configuration version Correct answer
  • Specifies the file path
  • Specifies the level

Explanation: The version key specifies the configuration version; usually 1.

3 Match the dictConfig concepts

Term Meaning
version Specifies the version
formatters Defines formatters
handlers Defines handlers
loggers Defines loggers

All lessons in Advanced