Add colours to the output of Python's logging module with ANSI escape codes and terminal support.
npx @tessl/cli install tessl/pypi-colorlog@6.9.0A Python logging formatter that adds color support to terminal output by extending the standard logging.Formatter class. Colorlog provides colored logging formatters with ANSI escape code support for terminals and Windows (via colorama integration), making log output more readable and easier to debug.
pip install colorlogimport colorlogFor basic colored logging:
from colorlog import ColoredFormatterFor enhanced logging functions:
from colorlog import basicConfig, getLoggerimport colorlog
# Quick setup with basicConfig
colorlog.basicConfig()
logger = colorlog.getLogger('example')
logger.debug("Debug message")
logger.info("Info message")
logger.warning("Warning message")
logger.error("Error message")
logger.critical("Critical message")For more control:
import colorlog
import logging
# Create colored formatter
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='%'
)
# Create handler and logger
handler = colorlog.StreamHandler()
handler.setFormatter(formatter)
logger = colorlog.getLogger('example')
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)Colorlog extends Python's standard logging system with color support:
The library maintains full compatibility with Python's logging module while adding color capabilities through ANSI escape codes, with automatic Windows support via colorama.
Core formatting classes that add color support to log messages. The main ColoredFormatter class extends Python's logging.Formatter with ANSI color codes, while LevelFormatter provides per-level formatting.
class ColoredFormatter(logging.Formatter):
def __init__(
self,
fmt: Optional[str] = None,
datefmt: Optional[str] = None,
style: str = "%",
log_colors: Optional[Dict[str, str]] = None,
reset: bool = True,
secondary_log_colors: Optional[Dict[str, Dict[str, str]]] = None,
validate: bool = True,
stream: Optional[IO[str]] = None,
no_color: bool = False,
force_color: bool = False,
defaults: Optional[Dict[str, Any]] = None,
): ...
class LevelFormatter:
def __init__(self, fmt: Dict[str, str], **kwargs: Any): ...Wrapper functions that enhance Python's standard logging functions with automatic configuration and colored output. These functions automatically set up ColoredFormatter when no handlers exist.
def basicConfig(
style: str = "%",
log_colors: Optional[Dict[str, str]] = None,
reset: bool = True,
secondary_log_colors: Optional[Dict[str, Dict[str, str]]] = None,
format: str = "%(log_color)s%(levelname)s%(reset)s:%(name)s:%(message)s",
datefmt: Optional[str] = None,
**kwargs
) -> None: ...
def debug(msg, *args, **kwargs): ...
def info(msg, *args, **kwargs): ...
def warning(msg, *args, **kwargs): ...
def error(msg, *args, **kwargs): ...
def critical(msg, *args, **kwargs): ...
def log(level, msg, *args, **kwargs): ...
def exception(msg, *args, **kwargs): ...ANSI escape code generation and color parsing system. Provides comprehensive color support including foreground/background colors, text styling, and 256-color terminal support.
# Available via colorlog.escape_codes submodule
def parse_colors(string: str) -> str: ...
# Color constants and mappings
escape_codes: Dict[str, str]
default_log_colors: Dict[str, str]# Type aliases for function signatures
from typing import IO, Dict, Mapping, Any, Optional
EscapeCodes = Mapping[str, str]
LogColors = Mapping[str, str]
SecondaryLogColors = Mapping[str, LogColors]# Log level constants (re-exported from logging)
CRITICAL: int
DEBUG: int
ERROR: int
FATAL: int
INFO: int
NOTSET: int
WARN: int
WARNING: int
# Default color mapping
default_log_colors: Dict[str, str]