Add colours to the output of Python's logging module.
Project description
Log formatting with colors!
Add colours to the output of Python's logging module.
Status
colorlog currently requires Python 3.6 or higher. Older versions (below 5.x.x) support Python 2.6 and above.
- colorlog 6.x requires Python 3.6 or higher.
- colorlog 5.x is an interim version that will warn Python 2 users to downgrade.
- colorlog 4.x is the final version supporting Python 2.
colorama is included as a required dependency and initialised when using colorlog on Windows.
This library is over a decade old and supported a wide set of Python versions for most of its life, which has made it a difficult library to add new features to. colorlog 6 may break backwards compatibility so that newer features can be added more easily, but may still not accept all changes or feature requests. colorlog 4 might accept essential bugfixes but should not be considered actively maintained and will not accept any major changes or new features.
Installation
Install from PyPI with:
pip install colorlog Several Linux distributions provide official packages (Debian, Arch, Fedora, Gentoo, OpenSuse and Ubuntu), and others have user provided packages (BSD ports, Conda).
Usage
import colorlog handler = colorlog.StreamHandler() handler.setFormatter(colorlog.ColoredFormatter( '%(log_color)s%(levelname)s:%(name)s:%(message)s')) logger = colorlog.getLogger('example') logger.addHandler(handler) The ColoredFormatter class takes several arguments:
format: The format string used to output the message (required).datefmt: An optional date format passed to the base class. Seelogging.Formatter.reset: Implicitly adds a color reset code to the message output, unless the output already ends with one. Defaults toTrue.log_colors: A mapping of record level names to color names. The defaults can be found incolorlog.default_log_colors, or the below example.secondary_log_colors: A mapping of names tolog_colorsstyle mappings, defining additional colors that can be used in format strings. See below for an example.style: Available on Python 3.2 and above. Seelogging.Formatter.
Color escape codes can be selected based on the log records level, by adding parameters to the format string:
log_color: Return the color associated with the records level.<name>_log_color: Return another color based on the records level if the formatter has secondary colors configured (seesecondary_log_colorsbelow).
Multiple escape codes can be used at once by joining them with commas when configuring the color for a log level (but can't be used directly in the format string). For example, black,bg_white would use the escape codes for black text on a white background.
The following escape codes are made available for use in the format string:
{color},fg_{color},bg_{color}: Foreground and background colors.bold,bold_{color},fg_bold_{color},bg_bold_{color}: Bold/bright colors.thin,thin_{color},fg_thin_{color}: Thin colors (terminal dependent).reset: Clear all formatting (both foreground and background colors).
The available color names are:
blackredgreenyellowblue,purplecyanwhite
You can also use "bright" colors. These aren't standard ANSI codes, and support for these varies wildly across different terminals.
light_blacklight_redlight_greenlight_yellowlight_bluelight_purplelight_cyanlight_white
Examples
The following code creates a ColoredFormatter for use in a logging setup, using the default values for each argument.
from colorlog import ColoredFormatter formatter = 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='%' ) Using secondary_log_colors
Secondary log colors are a way to have more than one color that is selected based on the log level. Each key in secondary_log_colors adds an attribute that can be used in format strings (message becomes message_log_color), and has a corresponding value that is identical in format to the log_colors argument.
The following example highlights the level name using the default log colors, and highlights the message in red for error and critical level log messages.
from colorlog import ColoredFormatter formatter = ColoredFormatter( "%(log_color)s%(levelname)-8s%(reset)s %(message_log_color)s%(message)s", secondary_log_colors={ 'message': { 'ERROR': 'red', 'CRITICAL': 'red' } } ) With dictConfig
logging.config.dictConfig({ 'formatters': { 'colored': { '()': 'colorlog.ColoredFormatter', 'format': "%(log_color)s%(levelname)-8s%(reset)s %(blue)s%(message)s" } } }) A full example dictionary can be found in tests/test_colorlog.py.
With fileConfig
... [formatters] keys=color [formatter_color] class=colorlog.ColoredFormatter format=%(log_color)s%(levelname)-8s%(reset)s %(bg_blue)s[%(name)s]%(reset)s %(message)s from fileConfig datefmt=%m-%d %H:%M:%S An instance of ColoredFormatter created with those arguments will then be used by any handlers that are configured to use the color formatter.
A full example configuration can be found in tests/test_config.ini.
With custom log levels
ColoredFormatter will work with custom log levels added with logging.addLevelName:
import logging, colorlog TRACE = 5 logging.addLevelName(TRACE, 'TRACE') formatter = colorlog.ColoredFormatter(log_colors={'TRACE': 'yellow'}) handler = logging.StreamHandler() handler.setFormatter(formatter) logger = logging.getLogger('example') logger.addHandler(handler) logger.setLevel('TRACE') logger.log(TRACE, 'a message using a custom level') Tests
Tests similar to the above examples are found in tests/test_colorlog.py.
Status
colorlog is in maintenance mode. I try and ensure bugfixes are published, but compatibility a wide set of Python versions makes this a difficult codebase to add features to. Any changes that might break backwards compatibility for existing users will not be considered.
Alternatives
There are some more modern libraries for improving Python logging you may find useful.
Projects using colorlog
GitHub provides a list of projects that depend on colorlog.
Some early adopters included Errbot, Pythran, and zenlog.
Licence
Copyright (c) 2012-2025 Sam Clements sam@borntyping.co.uk
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file colorlog-6.10.1.tar.gz.
File metadata
- Download URL: colorlog-6.10.1.tar.gz
- Upload date:
- Size: 17.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 | eb4ae5cb65fe7fec7773c2306061a8e63e02efc2c72eba9d27b0fa23c94f1321 | |
| MD5 | 177e72715015e36226abdcac193dc82a | |
| BLAKE2b-256 | a261f083b5ac52e505dfc1c624eafbf8c7589a0d7f32daa398d2e7590efa5fda |
Provenance
The following attestation bundles were made for colorlog-6.10.1.tar.gz:
Publisher: ci.yml on borntyping/python-colorlog
- Statement:
- Statement type:
https://in-toto.io/Statement/v1 - Predicate type:
https://docs.pypi.org/attestations/publish/v1 - Subject name:
colorlog-6.10.1.tar.gz - Subject digest:
eb4ae5cb65fe7fec7773c2306061a8e63e02efc2c72eba9d27b0fa23c94f1321 - Sigstore transparency entry: 613184417
- Sigstore integration time:
- Permalink:
borntyping/python-colorlog@68b10149ffdaf3e4f7798d8050cc66c3da53c8e0 - Branch / Tag:
refs/tags/v6.10.1 - Owner: https://github.com/borntyping
- Access:
public
- Token Issuer:
https://token.actions.githubusercontent.com - Runner Environment:
github-hosted - Publication workflow:
ci.yml@68b10149ffdaf3e4f7798d8050cc66c3da53c8e0 - Trigger Event:
push
- Statement type:
File details
Details for the file colorlog-6.10.1-py3-none-any.whl.
File metadata
- Download URL: colorlog-6.10.1-py3-none-any.whl
- Upload date:
- Size: 11.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 | 2d7e8348291948af66122cff006c9f8da6255d224e7cf8e37d8de2df3bad8c9c | |
| MD5 | 22e45fbe1d9cc42482ffa6207a5baa59 | |
| BLAKE2b-256 | 6dc1e419ef3723a074172b68aaa89c9f3de486ed4c2399e2dbd8113a4fdcaf9e |
Provenance
The following attestation bundles were made for colorlog-6.10.1-py3-none-any.whl:
Publisher: ci.yml on borntyping/python-colorlog
- Statement:
- Statement type:
https://in-toto.io/Statement/v1 - Predicate type:
https://docs.pypi.org/attestations/publish/v1 - Subject name:
colorlog-6.10.1-py3-none-any.whl - Subject digest:
2d7e8348291948af66122cff006c9f8da6255d224e7cf8e37d8de2df3bad8c9c - Sigstore transparency entry: 613184448
- Sigstore integration time:
- Permalink:
borntyping/python-colorlog@68b10149ffdaf3e4f7798d8050cc66c3da53c8e0 - Branch / Tag:
refs/tags/v6.10.1 - Owner: https://github.com/borntyping
- Access:
public
- Token Issuer:
https://token.actions.githubusercontent.com - Runner Environment:
github-hosted - Publication workflow:
ci.yml@68b10149ffdaf3e4f7798d8050cc66c3da53c8e0 - Trigger Event:
push
- Statement type: