or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

colors.mdformatters.mdindex.mdlogging-functions.md
tile.json

tessl/pypi-colorlog

Add colours to the output of Python's logging module with ANSI escape codes and terminal support.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/colorlog@6.9.x

To install, run

npx @tessl/cli install tessl/pypi-colorlog@6.9.0

index.mddocs/

Colorlog

A 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.

Package Information

  • Package Name: colorlog
  • Package Type: pypi
  • Language: Python
  • Installation: pip install colorlog
  • Python Version: 3.6+
  • Windows Support: Automatic via colorama

Core Imports

import colorlog

For basic colored logging:

from colorlog import ColoredFormatter

For enhanced logging functions:

from colorlog import basicConfig, getLogger

Basic Usage

import 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)

Architecture

Colorlog extends Python's standard logging system with color support:

  • ColoredFormatter: Main formatter class that adds color codes to log messages
  • LevelFormatter: Per-level format string support for different log levels
  • Escape Codes: ANSI color code generation and parsing system
  • Wrapper Functions: Enhanced logging functions with automatic configuration

The library maintains full compatibility with Python's logging module while adding color capabilities through ANSI escape codes, with automatic Windows support via colorama.

Capabilities

Colored Formatters

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): ...

Colored Formatters

Enhanced Logging Functions

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): ...

Enhanced Logging Functions

Color System

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]

Color System

Types

# 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]

Constants

# 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]