4

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.

aress31
  • 340
  • 5
  • 20

1 Answers1

5

After installing the coloredlogs module with

$ pip install coloredlogs

you are able to configure your console output to be colored. Usually this is done by have something like

coloredlogs.install(level='DEBUG', logger=logger)

in your code.

However, if you want to use coloredlogs together with the dictionary configuration you need to create a specific formatter like this:

import logging.config

logging.config.dictConfig(my_logging_dict)
logger = logging.getLogger(__name__)

my_logging_dict = {
    'version': 1,
    'disable_existing_loggers': True,   # set True to suppress existing loggers from other modules
    'loggers': {
        '': {
           'level': 'DEBUG',
           'handlers': ['console', 'file'],
        },
    },
    'formatters': {
        'colored_console': {
           '()': 'coloredlogs.ColoredFormatter', 
           'format': "%(asctime)s - %(name)s - %(levelname)s - %(message)s", 
           'datefmt': '%H:%M:%S'
        },
        'format_for_file': {
           'format': "%(asctime)s :: %(levelname)s :: %(funcName)s in %(filename)s (l:%(lineno)d) :: %(message)s", 
           'datefmt': '%Y-%m-%d %H:%M:%S'
        },
    'handlers': {
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'formatter': 'colored_console',
            'stream': 'ext://sys.stdout'
        },
        'file': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'formatter': 'format_for_file',
            'filename': log_file,
            'maxBytes': 500000,
            'backupCount': 5
        }
    },
}

How to create user defined objects for logging formatters is described in the logging docu. Note that the above code snippet is just an example of the configuration I am using.

Itachi
  • 5,777
  • 2
  • 37
  • 69
Fabs
  • 161
  • 1
  • 9