Skip to main content

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 dependency on Windows, where it is automatically initialised to support colored output.

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())

logger = colorlog.getLogger('example')
logger.addHandler(handler)

Arguments

ColoredFormatter extends logging.Formatter and accepts the following arguments:

  • fmt (default=None): A format string used to output the message. If None, a default format string is selected based on style (e.g. %(log_color)s%(levelname)s:%(name)s:%(message)s for % style).
  • datefmt, style, validate, defaults: see logging.Formatter.
  • reset (default=True): Implicitly adds a color reset code to the message output, unless the output already ends with one.
  • log_colors (default=None): A mapping of record level names to color names. If None, colorlog.default_log_colors is used.
  • secondary_log_colors (default=None): A mapping of names to log_colors style mappings, defining additional colors that can be used in format strings. If None, an empty mapping is used.
  • stream (default=None): The stream being written to (e.g. sys.stderr). Used to detect whether the output is a TTY. Colors are disabled automatically on non-TTY streams unless force_color is set.
  • no_color (default=False): Disable color output. Can also be set via the NO_COLOR environment variable.
  • force_color (default=False): Force color output even on non-TTY streams. Takes precedence over no_color. Can also be set via the FORCE_COLOR environment variable.

Color escape codes

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 (see secondary_log_colors below).

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:

  • black
  • red
  • green
  • yellow
  • blue
  • purple
  • cyan
  • white

You can also use "bright" colors. These aren't standard ANSI codes, and support for these varies wildly across different terminals.

  • light_black
  • light_red
  • light_green
  • light_yellow
  • light_blue
  • light_purple
  • light_cyan
  • light_white

In addition to pre-defined color names, you can use integers from 0 to 255 for 256-color support (e.g. fg_196, bg_42).

Examples

Example output

The following snippet creates a ColoredFormatter for use in a logging setup, with a custom format string and log colors:

from colorlog import ColoredFormatter

formatter = ColoredFormatter(
	"%(log_color)s%(levelname)-8s%(reset)s %(blue)s%(message)s",
	log_colors={
		"DEBUG": "cyan",
		"INFO": "green",
		"WARNING": "yellow",
		"ERROR": "red",
		"CRITICAL": "red,bg_white",
	}
)

See docs/example.py for full example script.

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.

Release files for colorlog 6.12.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for colorlog 6.12.0
File Size Uploaded
colorlog-6.12.0.tar.gz 18.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for colorlog 6.12.0
File Interpreter ABI Platform
colorlog-6.12.0-py3-none-any.whl Python 3 none any Details

Total release size: 30.4 kB

Release files / colorlog-6.12.0.tar.gz

Download URL colorlog-6.12.0.tar.gz
Size 18.2 kB
Tags Source
SHA-256 checksum
How to use checksums
2a7924c1dadf18b22a0eb8b06d1c7b01d5341707ec1641eb6fcc4fde0c3e8e5f
BLAKE2b-256 checksum
How to use checksums
8c55ba79756cb90c8d69d599d57785398ac87bba7b19c80e87f4e8a562197c93
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 23, 2026.

Transparency log

Release files / colorlog-6.12.0-py3-none-any.whl

Download URL colorlog-6.12.0-py3-none-any.whl
Size 12.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
30d392604e9110045a2c2aeefc27d7a017abbab63f3a8aee594eac0801df784e
BLAKE2b-256 checksum
How to use checksums
d4190b6647bf5e331521e55d2b63bfbdc210bd9cd605189273f03614a05f702d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 23, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

6.12.0 This release

2 release files

6.11.0

2 release files

6.10.1

2 release files

6.9.0

2 release files

6.8.2

2 release files

6.8.0

2 release files

6.7.0

2 release files

6.6.0

2 release files

6.5.0

2 release files

6.4.1

2 release files

6.4.0

2 release files

5.0.1

2 release files

5.0.0

2 release files

4.8.0

2 release files

4.7.2

2 release files

4.7.0

2 release files

4.6.2

2 release files

4.6.0

1 release file

4.5.0

2 release files

4.4.0

2 release files

4.3.0

2 release files

4.2.1

2 release files

4.2.0

2 release files

4.1.0

2 release files

4.0.2

2 release files

4.0.0

2 release files

3.2.0

2 release files

3.1.4

2 release files

3.1.3

2 release files

3.1.2

2 release files

3.1.1

2 release files

3.1.0

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.10.0

2 release files

2.9.0

2 release files

2.8.0

2 release files

2.7.0

2 release files

2.6.3

1 release file

2.6.2

2 release files

2.6.1

2 release files

2.6.0

2 release files

2.5.0

2 release files

2.4.0

2 release files

2.3.1

2 release files

2.3.0

2 release files

2.2.0

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.0

1 release file

1.8

1 release file

1.7

1 release file

1.6

1 release file

1.5

1 release file

1.4

1 release file

1.3

1 release file

1.2

1 release file

1.1

1 release file

1.0

1 release file

0.6

1 release file

0.5

1 release file

0.4

1 release file

0.3

1 release file

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page