1import logging
2import logging.config
3from os import environ
4
5
6def configure_logging(logging_settings):
7 # Load the defaults
8 default_logging = {
9 "version": 1,
10 "disable_existing_loggers": False,
11 "formatters": {
12 "simple": {
13 "format": "[%(levelname)s] %(message)s",
14 },
15 },
16 "handlers": {
17 "plain_console": {
18 "level": environ.get("PLAIN_LOG_LEVEL", "INFO"),
19 "class": "logging.StreamHandler",
20 "formatter": "simple",
21 },
22 "app_console": {
23 "level": environ.get("APP_LOG_LEVEL", "INFO"),
24 "class": "logging.StreamHandler",
25 "formatter": "simple",
26 },
27 },
28 "loggers": {
29 "plain": {
30 "handlers": ["plain_console"],
31 "level": environ.get("PLAIN_LOG_LEVEL", "INFO"),
32 },
33 "app": {
34 "handlers": ["app_console"],
35 "level": environ.get("APP_LOG_LEVEL", "INFO"),
36 "propagate": False,
37 },
38 },
39 }
40 logging.config.dictConfig(default_logging)
41
42 # Then customize it from settings
43 if logging_settings:
44 logging.config.dictConfig(logging_settings)