Registry / observability / coloredlogs

coloredlogs

JSON →
library15.0.1pypypi✓ verified 49d ago

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.

observability
pip install coloredlogs
Install & Compatibility
Where this runs
tested against v15.0.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.950 runs
installs and imports cleanly · install 0.0s · import 0.126s · 18.8MB
glibc
py 3.103.950 runs
installs and imports cleanly · install 1.7s · import 0.120s · 19MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

coloredlogs
import coloredlogs, logging
The primary interaction is through module-level functions like `coloredlogs.install()`.
ColoredFormatter
from coloredlogs import ColoredFormatter
While `ColoredFormatter` can be imported and used directly, `coloredlogs.install()` is the recommended and simpler way to integrate colored logging as it handles handler setup.

This quickstart demonstrates how to import `coloredlogs` and the standard `logging` module, create a logger, and then apply colored output using `coloredlogs.install()` to a specific logger with a 'DEBUG' level. Log messages at different severity levels will then be displayed with corresponding colors in the terminal.

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')
Debug
Known issues
breakingIn version 15.0, `coloredlogs` stopped enabling system logging by default on macOS and Windows due to problematic behavior. This is a backwards incompatible change.
fix
If you previously relied on automatic system logging on these platforms, you may need to explicitly configure it or adjust your logging setup.
affects: 15.0 and later
breakingStarting 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.
fix
For Windows installations prior to version 10.0.14393 or if native ANSI support is not working as expected, explicitly install `colorama`: `pip install colorama`.
affects: 14.0 and later
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`.
fix
Always pass a specific logger instance to `coloredlogs.install(logger=my_logger)` if you only want to affect that logger. If you intend to modify the root logger, be aware of the default `INFO` level and explicitly set `logging.getLogger().setLevel(logging.WARNING)` if Python's default `WARNING` level is desired for other parts of your application after `coloredlogs.install()` has run.
affects: All versions
breakingIn 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.
fix
If migrating from versions older than 6.0, explicitly set `level='DEBUG'` in `coloredlogs.install()` or `coloredlogs.install(syslog='debug')` if `DEBUG` verbosity is desired.
affects: 6.0 and later
gotchaWhen `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.
fix
This is intended behavior. If colored output in files is desired (e.g., for viewing in Vim with specific plugins or converting to HTML), specific tools or configurations are needed, such as `coloredlogs --to-html your-command` for HTML output.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'coloredlogs'
The 'coloredlogs' package is not installed in the Python environment being used, or there's a mismatch between the Python interpreter running the code and the one where the package was installed.
fix
Ensure the package is installed using pip: `pip install coloredlogs` or, for more robust environment management, `python -m pip install coloredlogs`.
coloredlogs not displaying colors (e.g., in Docker, PyCharm, or when redirecting output)
By default, 'coloredlogs' automatically detects if the output stream is an interactive terminal (TTY). In non-TTY environments like Docker containers, some IDE consoles, or when redirecting output to a file, it suppresses ANSI escape codes, leading to no colors or raw escape characters in file outputs.
fix
To force color output in non-TTY environments, pass `isatty=True` to `coloredlogs.install()`. For PyCharm, also specify `stream=sys.stdout`. For Docker, ensure the container is run with a TTY (`docker run -it`). For viewing logs in files, use a TTY-aware viewer like `less -R <logfile>` or convert to HTML using `coloredlogs --to-html`.

```python
import coloredlogs, logging, sys

# Force colors and ensure output goes to stdout
coloredlogs.install(level='DEBUG', isatty=True, stream=sys.stdout)
```
coloredlogs debug messages not showing (or other log levels not appearing)
The Python `logging` module's root logger has a default level of `WARNING`. While `coloredlogs.install()` sets up its own handler, messages below the effective level of the root logger (or any other handler in the chain) will be filtered out before 'coloredlogs' can process them. Also, if a specific logger object is passed to `coloredlogs.install()`, only messages from that logger will be colored, not from other parts of the application or the root logger.
fix
Explicitly set the desired logging level for the root logger or the specific logger you are configuring before (or within) `coloredlogs.install()`.

```python
import coloredlogs, logging

# Configure the root logger to show DEBUG messages
logging.basicConfig(level=logging.DEBUG)
coloredlogs.install(level='DEBUG')

# Alternatively, if targeting a specific logger:
# logger = logging.getLogger('my_app')
# logger.setLevel(logging.DEBUG)
# coloredlogs.install(level='DEBUG', logger=logger)
```
coloredlogs custom levelname style not working or 'levelname' not appearing in log output
Customization of the `levelname` field's style may not take effect if the `fmt` string passed to `coloredlogs.install()` does not include `%(levelname)s`, or if the style override is not applied correctly to `coloredlogs.DEFAULT_FIELD_STYLES`.
fix
Ensure the format string explicitly includes `%(levelname)s` and modify `coloredlogs.DEFAULT_FIELD_STYLES` directly to apply custom styles before calling `install()`.

```python
import coloredlogs, logging

# Customize the style for the 'levelname' field
coloredlogs.DEFAULT_FIELD_STYLES['levelname'] = {'color': 'magenta', 'bold': True}

# Install coloredlogs with a format string that includes levelname
coloredlogs.install(
    level='INFO',
    fmt='%(asctime)s %(levelname)s %(message)s'
)
```
Upgrade
Version history
15.0.1latest on PyPI
Audit
Dependencies
humanfriendlyrequiredRequired for general functionality and date/time formatting.
coloramaoptionalOptional for enabling native ANSI escape sequence support on older Windows versions (pre-Windows 10 build 14393). It is automatically used if installed.
captureroptionalRequired when installing with the 'cron' extra for capturing output.
Agent activity
72 hits · last 30 days
node
4
mj12bot
4
seranking-bot
4
ahrefsbot
3
bytedance
2
Amazon
1
amazonbot
1
Resources