colorlog

6.10.1 · active · verified Sat Mar 28

colorlog is a Python library that adds colors to the output of Python's standard logging module. It currently stands at version 6.10.1 and is in active maintenance, focusing on bug fixes and Python 3 compatibility, rather than major feature additions that might introduce breaking changes.

Warnings

Install

Imports

Quickstart

This example demonstrates how to set up a basic logger with `colorlog.ColoredFormatter` to get colorized output. It defines custom colors for different logging levels and then logs messages at various severities. Alternatively, for a simpler setup, `colorlog.basicConfig()` can be used, which internally sets up a `ColoredFormatter` for the root logger.

import logging
import colorlog

# Configure a ColoredFormatter
formatter = colorlog.ColoredFormatter(
    '%(log_color)s%(levelname)-8s%(reset)s %(blue)s%(message)s',
    datefmt=None,
    reset=True,
    log_colors={
        'DEBUG':    'cyan',
        'INFO':     'green',
        'WARNING':  'yellow',
        'ERROR':    'red',
        'CRITICAL': 'red,bg_white'
    },
    secondary_log_colors={},
    style='%'
)

# Get the root logger and set its level
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

# Add a StreamHandler with the colored formatter
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)

# Example log messages
logger.debug('This is a debug message')
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')
logger.critical('This is a critical message')

view raw JSON →