Coloredlogs: Colored Terminal Output for Python's Logging Module
coloredlogs is a Python library that enhances the default logging output by adding color formatting to log messages. It provides a simple way to make log messages more visually distinguishable, making it easier to read and interpret log output, especially when working in a terminal or command-line environment. The current version is 15.0.1. It is actively maintained with regular updates.
Warnings
- breaking In version 15.0, `coloredlogs` stopped enabling system logging by default on macOS and Windows due to problematic behavior. This is a backwards incompatible change.
- breaking Starting with version 14.0, `coloredlogs` dropped `colorama` as a *required* dependency for Windows 10 users, integrating native ANSI support instead. While `colorama` will still be used if installed, its implicit presence or absence can cause issues if not considered, especially for older Windows versions or specific environments.
- gotcha `coloredlogs.install()`, when called without a specific `logger` argument, applies its configuration to the *root logger*. This can unintentionally change the effective logging level for *all* loggers in your application, including those from third-party libraries, potentially silencing or making logs unexpectedly verbose. The default log level for the root logger is `INFO` when using `coloredlogs`, which differs from Python's default `WARNING`.
- breaking In release 6.0, `coloredlogs.install()` changed its default root logger level handling and `enable_system_logging()`'s default logging level changed from `DEBUG` to `INFO`. This affected the verbosity of logs for users migrating from older versions.
- gotcha When `coloredlogs` is active, and logging output is redirected to a file, the file will contain plain text without ANSI escape sequences (colors) by default, because `coloredlogs` detects it's not attached to an interactive terminal. This is by design, but can be a 'gotcha' if users expect colored log files for later viewing.
Install
-
pip install coloredlogs -
pip install 'coloredlogs[cron]'
Imports
- coloredlogs
import coloredlogs, logging
- ColoredFormatter
from coloredlogs import ColoredFormatter
Quickstart
import coloredlogs, logging
# Create a logger object. By default, coloredlogs.install()
# will apply to the root logger if no logger is specified.
logger = logging.getLogger(__name__)
# Install coloredlogs on the logger.
# You can also pass level='INFO' or other levels.
coloredlogs.install(level='DEBUG', logger=logger)
# Some examples of 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')