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.
pip install coloredlogsVerified import paths — ran on the pinned version, not inferred.
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.
If you previously relied on automatic system logging on these platforms, you may need to explicitly configure it or adjust your logging setup.
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`.
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.
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.
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.
Ensure the package is installed using pip: `pip install coloredlogs` or, for more robust environment management, `python -m pip install coloredlogs`.
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) ```
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)
```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'
)
```