{"id":471,"library":"colorlog","title":"colorlog","description":"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.","status":"active","version":"6.10.1","language":"python","source_language":"en","source_url":"https://github.com/borntyping/python-colorlog","tags":["logging","colors","terminal","CLI","developer-tool"],"install":[{"cmd":"pip install colorlog","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Provides cross-platform ANSI color support, particularly on Windows. colorlog handles its initialization internally.","package":"colorama","optional":true}],"imports":[{"symbol":"basicConfig","correct":"import colorlog\ncolorlog.basicConfig(...)"},{"note":"In versions 6.3.0a1 and later, the internal 'colorlog' module was renamed to 'formatter'. Direct import from 'colorlog.colorlog' is deprecated.","wrong":"from colorlog.colorlog import ColoredFormatter","symbol":"ColoredFormatter","correct":"from colorlog import ColoredFormatter"}],"quickstart":{"code":"import logging\nimport colorlog\n\n# Configure a ColoredFormatter\nformatter = colorlog.ColoredFormatter(\n    '%(log_color)s%(levelname)-8s%(reset)s %(blue)s%(message)s',\n    datefmt=None,\n    reset=True,\n    log_colors={\n        'DEBUG':    'cyan',\n        'INFO':     'green',\n        'WARNING':  'yellow',\n        'ERROR':    'red',\n        'CRITICAL': 'red,bg_white'\n    },\n    secondary_log_colors={},\n    style='%'\n)\n\n# Get the root logger and set its level\nlogger = logging.getLogger(__name__)\nlogger.setLevel(logging.DEBUG)\n\n# Add a StreamHandler with the colored formatter\nhandler = logging.StreamHandler()\nhandler.setFormatter(formatter)\nlogger.addHandler(handler)\n\n# Example log messages\nlogger.debug('This is a debug message')\nlogger.info('This is an info message')\nlogger.warning('This is a warning message')\nlogger.error('This is an error message')\nlogger.critical('This is a critical message')","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade Python to 3.6+ or pin `colorlog<5` for Python 2.x or `colorlog<6` for Python 3.5.","message":"colorlog v6.x dropped support for Python 2 and Python versions older than 3.6. Users needing compatibility with older Python versions (e.g., Python 2.x or 3.5) must pin their dependency to `colorlog<5` or `colorlog<6` respectively.","severity":"breaking","affected_versions":">=6.0.0"},{"fix":"Replace `TTYColoredFormatter` with `ColoredFormatter`.","message":"The internal `TTYColoredFormatter` class was merged into `ColoredFormatter` in version 6.0.0-alpha.2. Direct references to `TTYColoredFormatter` will no longer work.","severity":"breaking","affected_versions":">=6.0.0a2"},{"fix":"Ensure `ColoredFormatter` is only used with handlers outputting to terminals (e.g., `logging.StreamHandler`). For file logging, use a standard `logging.Formatter` or configure distinct handlers for terminal vs. file output. You can also use the `no_color` argument or the `NO_COLOR` environment variable to disable colors.","message":"Logging output redirected to a file or a non-terminal environment will include raw ANSI escape codes, making the file unreadable unless processed by an ANSI-aware viewer. colorlog attempts to detect terminal capabilities but might fail in some setups.","severity":"gotcha","affected_versions":"All"},{"fix":"Be aware of these environment variables when troubleshooting unexpected color behavior in different environments.","message":"The `FORCE_COLOR` environment variable can override color detection, forcing colors even if `colorlog` thinks it's not a TTY. Conversely, the `NO_COLOR` environment variable (following the no-color.org standard) can disable colors.","severity":"gotcha","affected_versions":">=6.0.0a2 (NO_COLOR), >=6.6.0 (FORCE_COLOR)"},{"fix":"Always import `ColoredFormatter` directly from the top-level `colorlog` package (e.g., `from colorlog import ColoredFormatter`).","message":"The internal module `colorlog.colorlog` was renamed to `colorlog.formatter`. While direct import from `colorlog.colorlog` might still work for some time due to internal mechanisms, it is considered an internal detail and should not be relied upon.","severity":"deprecated","affected_versions":">=6.3.0a1"}],"env_vars":null,"last_verified":"2026-05-12T14:04:46.103Z","next_check":"2026-06-26T00:00:00.000Z","problems":[{"fix":"Install the colorlog package using pip: `pip install colorlog`","cause":"The colorlog package has not been installed in the current Python environment or the environment where the script is being executed.","error":"ModuleNotFoundError: No module named 'colorlog'"},{"fix":"Ensure `colorlog` is imported and its `basicConfig` (or `ColoredFormatter` on handlers) is applied *before* any `logging.basicConfig()` calls if you want it to manage the root logger. Verify your terminal supports ANSI escape codes (e.g., using a modern terminal or `TERM=xterm-256color`).\n\n```python\nimport colorlog\nimport logging\n\n# Best practice: use colorlog.basicConfig directly\ncolorlog.basicConfig(level=logging.DEBUG,\n                    format='%(log_color)s%(levelname)-8s%(reset)s %(message)s')\n\n# Or, if you need to use logging.basicConfig first, then apply ColoredFormatter manually\n# logging.basicConfig(level=logging.DEBUG)\n# handler = colorlog.StreamHandler()\n# handler.setFormatter(colorlog.ColoredFormatter('%(log_color)s%(levelname)-8s%(reset)s %(message)s'))\n# logging.getLogger().addHandler(handler)\n\nlogger = logging.getLogger(__name__)\nlogger.debug('This is a debug message')\nlogger.info('This is an info message')\nlogger.warning('This is a warning message')\nlogger.error('This is an error message')\n```","cause":"This issue often arises when the terminal or IDE console does not support ANSI escape codes, or when `logging.basicConfig()` is called before `colorlog` is imported and configured, preventing `colorlog` from properly wrapping stream handlers. On Windows, ensure colorama (a dependency of colorlog) is correctly initializing.","error":"colorlog not showing colors"},{"fix":"Use a standard `logging.Formatter` for `FileHandler`s to ensure log files contain plain text, while continuing to use `colorlog.ColoredFormatter` for `StreamHandler`s (console output).\n\n```python\nimport colorlog\nimport logging\n\n# Configure a logger\nlogger = logging.getLogger(__name__)\nlogger.setLevel(logging.DEBUG)\n\n# Console handler with colors\nconsole_handler = colorlog.StreamHandler()\nconsole_formatter = colorlog.ColoredFormatter(\n    '%(log_color)s%(levelname)-8s%(reset)s %(message)s',\n    log_colors={\n        'DEBUG': 'cyan',\n        'INFO': 'green',\n        'WARNING': 'yellow',\n        'ERROR': 'red',\n        'CRITICAL': 'red,bg_white',\n    }\n)\nconsole_handler.setFormatter(console_formatter)\nlogger.addHandler(console_handler)\n\n# File handler without colors\nfile_handler = logging.FileHandler('app.log')\nfile_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')\nfile_handler.setFormatter(file_formatter)\nlogger.addHandler(file_handler)\n\nlogger.debug('This is a debug message')\nlogger.info('This is an info message')\nlogger.warning('This is a warning message')\nlogger.error('This is an error message')\n```","cause":"When a `FileHandler` is configured to use `colorlog.ColoredFormatter`, the ANSI escape codes meant for terminal colorization are written directly into the plain text log file, making the file unreadable as it does not interpret these codes.","error":"colorlog raw escape codes in log file"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"install_tag":"verified","quickstart_score":80,"quickstart_tag":"verified","pypi_latest":null,"install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.03,"mem_mb":1.4,"disk_size":"17.8M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":1.4,"disk_size":"18M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.06,"mem_mb":1.6,"disk_size":"19.7M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.05,"mem_mb":1.6,"disk_size":"20M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.04,"mem_mb":1.4,"disk_size":"11.6M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.04,"mem_mb":1.4,"disk_size":"12M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.04,"mem_mb":1.7,"disk_size":"11.2M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.04,"mem_mb":1.5,"disk_size":"12M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.04,"mem_mb":1.4,"disk_size":"17.3M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":1.4,"disk_size":"18M"}]},"quickstart_checks":{"last_tested":"2026-04-23","tag":"verified","tag_description":"quickstart runs on critical runtimes, recently tested","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}}