Using logging with coloredlogs

Viewed 5250

I am trying to create a json configuration file to load with logging.config.dictConfig() using the coloredlogs library for a colored output.

I am getting the following error:

Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\logging\config.py", line 538, in configure
    formatters[name])
  File "C:\Program Files\Python36\lib\logging\config.py", line 669, in configure_formatter
    result = c(fmt, dfmt, style)
  File "C:\Program Files\Python36\lib\site-packages\coloredlogs\__init__.py", line 834, in __init__
    self.level_styles = self.nn.normalize_keys(DEFAULT_LEVEL_STYLES if level_styles is None else level_styles)
  File "C:\Program Files\Python36\lib\site-packages\coloredlogs\__init__.py", line 1111, in normalize_keys
    return dict((self.normalize_name(k), v) for k, v in value.items())
AttributeError: 'str' object has no attribute 'items'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ".\sci.py", line 205, in <module>
    main()
  File ".\sci.py", line 180, in main
    logging.config.dictConfig(json.load(json_config))
  File "C:\Program Files\Python36\lib\logging\config.py", line 795, in dictConfig
    dictConfigClass(config).configure()
  File "C:\Program Files\Python36\lib\logging\config.py", line 541, in configure
    'formatter %r: %s' % (name, e))
ValueError: Unable to configure formatter 'colored': 'str' object has no attribute 'items'

My configuration file looks as follows:

{
    "version": 1,
    "disable_existing_loggers": true,

    "formatters": {
        "colored": {
            "class": "coloredlogs.ColoredFormatter",
            "datefmt": "%H:%M:%S",
            "format": "%(asctime)s %(module)-16s: %(levelname)-8s %(message)s"
        }
    },

    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "formatter": "colored",
            "level": "DEBUG",
            "stream": "ext://sys.stdout"
        }
    },

    "loggers": {
    },

    "root": {
        "handlers": [
            "console"
        ],
        "level": "DEBUG"
    }
}

The documentation about the use of coloredlogs with logging.config.dictConfig() is literally not existent.

1 Answers
Related